Skip to content

Commit

Permalink
update the getDefaultLogger to respect AXIOM_LOG_LEVEL
Browse files Browse the repository at this point in the history
  • Loading branch information
dasfmi committed Feb 26, 2024
1 parent cdf7315 commit 387f9ed
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
6 changes: 4 additions & 2 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogLevel } from './levels';
import { LOG_LEVEL, LogLevel } from './levels';
import { Logger } from './logger';
import { ConsoleTransport } from './transports/console';

Expand All @@ -7,8 +7,10 @@ export { LogEvent } from './event'
export { LogLevel } from './levels'

export function getDefaultLogger(): Logger {
const level = LogLevel[LOG_LEVEL as keyof typeof LogLevel];

return new Logger({
logLevel: LogLevel.debug,
logLevel: level,
autoFlush: true,
transformers: [],
transport: new ConsoleTransport(),
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/levels.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const LOG_LEVEL = process.env.NEXT_PUBLIC_AXIOM_LOG_LEVEL || 'debug';
export const LOG_LEVEL = process.env.NEXT_PUBLIC_AXIOM_LOG_LEVEL || process.env.AXIOM_LOG_LEVEL || 'debug';

export enum LogLevel {
debug = 0,
Expand Down
96 changes: 52 additions & 44 deletions packages/logger/test/unit/logLevels.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect, vi } from 'vitest';
import { getDefaultLogger, Logger, LogLevel } from '../src/index.ts';
import { ConsoleTransport } from '../src/transports/console.ts';
import { describe, it, expect, vi } from 'vitest';
import { getDefaultLogger, Logger, LogLevel } from '../../src/index';
import { ConsoleTransport } from '../../src/transports/console';

vi.hoisted(() => {
// stub axiom env vars before importing logger
Expand All @@ -10,48 +10,56 @@ vi.hoisted(() => {

vi.useFakeTimers();

test('log levels', async () => {
const mockedConsole = vi.spyOn(console, 'log');

const log = getDefaultLogger();
describe('Testing log levels', () => {
it('detects environment variable log level', async () => {
const mockedConsole = vi.spyOn(console, 'log');
const log = getDefaultLogger();

log.info('test');
await log.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);
log.config.logLevel = LogLevel.error;

// test overriding log level per logger
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);
log.info('test');
await log.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);
})

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.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.error('no logs');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);

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.warn('hello');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(2);
});
it('log levels', async () => {
const mockedConsole = vi.spyOn(console, 'log');

// test overriding log level per logger
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.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.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.error('no logs');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(0);

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.warn('hello');
await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(2);
});
})

0 comments on commit 387f9ed

Please sign in to comment.