Skip to content

Commit 4bb3465

Browse files
committed
Move runtime into BaseLogger, fix #256
1 parent aeb3620 commit 4bb3465

File tree

8 files changed

+16
-21
lines changed

8 files changed

+16
-21
lines changed

examples/nodejs/index2.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Logger, BaseLogger } from "../../src/index.js";
2-
import { Runtime } from "../../src/index.js";
32

43
const defaultLogObject: {
54
name: string;
@@ -23,7 +22,7 @@ logger.fatal("test1 %s test3", "test2");
2322

2423
console.log("###############");
2524

26-
const baseLogger = new BaseLogger(Runtime, {}, defaultLogObject);
25+
const baseLogger = new BaseLogger({}, defaultLogObject);
2726

2827
baseLogger.log(0, "test", "test base logger", { haha: true, password: "123456" }, ["SECRET"]);
2928

src/BaseLogger.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { formatTemplate } from "./formatTemplate.js";
22
import { formatNumberAddZeros } from "./formatNumberAddZeros.js";
3-
import { ISettingsParam, ISettings, ILogObjMeta, ILogObj, IErrorObject, Runtime, IMeta } from "./interfaces.js";
3+
import { ISettingsParam, ISettings, ILogObjMeta, ILogObj, IErrorObject, IRuntime, IMeta } from "./interfaces.js";
44
import { urlToObject } from "./urlToObj.js";
5+
import Runtime from "./runtime/nodejs/index.js";
6+
57
export * from "./interfaces.js";
8+
export { Runtime };
69

710
export class BaseLogger<LogObj> {
8-
private readonly runtime: Runtime;
11+
private readonly runtime: IRuntime;
912
public settings: ISettings<LogObj>;
1013
// not needed yet
1114
//private subLoggers: BaseLogger<LogObj>[] = [];
1215

13-
constructor(runtime: Runtime, settings?: ISettingsParam<LogObj>, private logObj?: LogObj, private stackDepthLevel: number = 4) {
14-
this.runtime = runtime;
16+
constructor(settings?: ISettingsParam<LogObj>, private logObj?: LogObj, private stackDepthLevel: number = 4) {
17+
this.runtime = Runtime;
1518

1619
this.settings = {
1720
type: settings?.type ?? "pretty",

src/index.browser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { BaseLogger } from "./BaseLogger.js";
22
import { ILogObj, ILogObjMeta, ISettingsParam } from "./interfaces.js";
3-
import BrowserRuntime from "./runtime/browser/index.js";
43

54
export * from "./interfaces.js";
65
export * from "./BaseLogger.js";
7-
export { BrowserRuntime };
86

97
export class Logger<LogObj> extends BaseLogger<LogObj> {
108
constructor(settings?: ISettingsParam<LogObj>, logObj?: LogObj) {
@@ -18,7 +16,7 @@ export class Logger<LogObj> extends BaseLogger<LogObj> {
1816
// style only for blink browsers
1917
settings.stylePrettyLogs = settings.stylePrettyLogs && isBrowser && !isBrowserBlinkEngine ? false : settings.stylePrettyLogs;
2018

21-
super(BrowserRuntime, settings, logObj, isSafari ? 4 : 5);
19+
super(settings, logObj, isSafari ? 4 : 5);
2220
}
2321

2422
/**

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { BaseLogger } from "./BaseLogger.js";
22
import { ILogObj, ILogObjMeta, ISettingsParam } from "./interfaces.js";
3-
import Runtime from "./runtime/nodejs/index.js";
4-
53
export * from "./interfaces.js";
64
export * from "./BaseLogger.js";
7-
export { Runtime };
85

96
declare global {
107
interface Window {
@@ -22,7 +19,7 @@ export class Logger<LogObj> extends BaseLogger<LogObj> {
2219
// style only for blink browsers
2320
settings.stylePrettyLogs = settings.stylePrettyLogs && isBrowser && !isBrowserBlinkEngine ? false : settings.stylePrettyLogs;
2421

25-
super(Runtime, settings, logObj, isSafari ? 4 : 5);
22+
super(settings, logObj, isSafari ? 4 : 5);
2623
}
2724

2825
/**

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export interface IMeta extends IMetaStatic {
186186
path?: IStackFrame;
187187
}
188188

189-
export interface Runtime {
189+
export interface IRuntime {
190190
getMeta: (
191191
logLevelId: number,
192192
logLevelName: string,

src/runtime/browser/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ILogObjMeta, ISettings, IStackFrame, Runtime } from "../../interfaces.js";
1+
import { ILogObjMeta, ISettings, IStackFrame, IRuntime } from "../../interfaces.js";
22
import { formatTemplate } from "../../formatTemplate.js";
33
import { formatWithOptions, InspectOptions } from "./util.inspect.polyfil.js";
44
import { jsonStringifyRecursive } from "./helper.jsonStringifyRecursive.js";
@@ -15,7 +15,7 @@ export default {
1515
isError,
1616
prettyFormatLogObj,
1717
prettyFormatErrorObj,
18-
} as Runtime;
18+
} as IRuntime;
1919

2020
export interface IMetaStatic {
2121
name?: string;

src/runtime/nodejs/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { hostname } from "os";
22
import { normalize as fileNormalize } from "path";
33
import { types, formatWithOptions, InspectOptions } from "util";
4-
import { Runtime, ILogObjMeta, ISettings, IStackFrame } from "../../interfaces.js";
4+
import { IRuntime, ILogObjMeta, ISettings, IStackFrame } from "../../interfaces.js";
55
import { formatTemplate } from "../../formatTemplate.js";
6-
import { stdout } from "process";
76
export { InspectOptions };
87

98
export default {
@@ -16,7 +15,7 @@ export default {
1615
isError,
1716
prettyFormatLogObj,
1817
prettyFormatErrorObj,
19-
} as Runtime;
18+
} as IRuntime;
2019

2120
export interface IMetaStatic {
2221
name?: string;

tests/Nodejs/8_json_LogObj.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import "ts-jest";
22
import { Logger, BaseLogger } from "../../src";
33
import { getConsoleLog, mockConsoleLog } from "./helper.js";
4-
import NodeRuntime from "../../src/runtime/nodejs/index";
54

65
interface ILogObj {
76
name: string;
@@ -17,7 +16,7 @@ describe("JSON: LogObj", () => {
1716
const defaultLogObject: ILogObj = {
1817
name: "test",
1918
};
20-
const logger = new BaseLogger<ILogObj>(NodeRuntime, { type: "json" }, defaultLogObject);
19+
const logger = new BaseLogger<ILogObj>({ type: "json" }, defaultLogObject);
2120
const logMsg = logger.log(1234, "testLevel", "Test");
2221
expect(logMsg?.name).toContain(defaultLogObject.name);
2322
expect(getConsoleLog()).toContain(`"name":"test",`);

0 commit comments

Comments
 (0)