Skip to content

Commit

Permalink
use transport inside the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dasfmi committed Feb 9, 2024
1 parent 243b369 commit 7b78107
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ dist
.vscode
.envrc
.turbo
.rollup.cache/
4 changes: 2 additions & 2 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
},
"homepage": "https://github.com/axiomhq/axiom-js/blob/main/packages/logger/README.md",
"dependencies": {
"@axiomhq/runtime": "*"
"@axiomhq/runtime": "workspace:*"
},
"peerDependencies": {
"@axiomhq/js": "*"
"@axiomhq/js": "workspace:*"
},
"exports": {
"types": "./dist/esm/types/index.d.ts",
Expand Down
4 changes: 1 addition & 3 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ export type LoggerConfig = {
};

export class Logger {
public logEvents: LogEvent[] = [];
children: Logger[] = [];


constructor(public config: LoggerConfig) {
if (this.config.logLevel == undefined || this.config.logLevel < 0) {
this.config.logLevel = LogLevel[LOG_LEVEL as keyof typeof LogLevel];
Expand Down Expand Up @@ -71,7 +69,7 @@ export class Logger {
logEvent = t.transform(logEvent);
}

this.logEvents.push(logEvent);
this.config.transport.log(logEvent);
if (this.config.autoFlush) {
this.flush();
}
Expand Down
12 changes: 6 additions & 6 deletions packages/logger/test/logLevels.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ test('log levels', async () => {
expect(mockedConsole).toHaveBeenCalledTimes(0);

// test overriding log level per logger
let logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.error, transport: new ConsoleTransport(), transformers: []});
let logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.error, transport: new ConsoleTransport(), transformers: [] });
logger.debug('hello');
logger.info('hello');
logger.warn('hello');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);

logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.warn, transport: new ConsoleTransport(), transformers: []});
logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.warn, transport: new ConsoleTransport(), transformers: [] });
logger.info('hello');
logger.debug('hello');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);

logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.info, transport: new ConsoleTransport(), transformers: []});
logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.info, transport: new ConsoleTransport(), transformers: [] });
logger.debug('hello');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);

// disabled logging
logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.off, transport: new ConsoleTransport(), transformers: []});
logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.off, transport: new ConsoleTransport(), transformers: [] });
logger.error('no logs');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);

logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.error, transport: new ConsoleTransport(), transformers: []});
logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.error, transport: new ConsoleTransport(), transformers: [] });
logger.warn('warn');
logger.error('error');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(1);

logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.debug, transport: new ConsoleTransport(), transformers: []});
logger = new Logger({ args: {}, autoFlush: false, logLevel: LogLevel.debug, transport: new ConsoleTransport(), transformers: [] });
logger.warn('hello');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(2);
Expand Down
15 changes: 5 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b78107

Please sign in to comment.