Skip to content

Commit

Permalink
Merge pull request #83 from axiomhq/islam/dx-391-add-log-level-to-nex…
Browse files Browse the repository at this point in the history
…t-axiom

configurable log levels per logger instance
  • Loading branch information
schehata committed Nov 30, 2022
2 parents dd26d40 + 26dd638 commit f572a65
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
47 changes: 47 additions & 0 deletions __tests__/logLevels.test.ts
@@ -0,0 +1,47 @@
/**
* @jest-environment jsdom
*/
// set axiom env vars before importing logger
process.env.AXIOM_INGEST_ENDPOINT = 'https://example.co/api/test';
process.env.AXIOM_LOG_LEVEL = 'error';
import { log, Logger } from '../src/logger';

jest.useFakeTimers();

test('log levels', async () => {
global.fetch = jest.fn() as jest.Mock;

log.info('test');
await log.flush();
expect(fetch).toHaveBeenCalledTimes(0);

// test overriding log level per logger
let logger = new Logger({}, null, false, 'frontend', 'error');
logger.debug('hello');
logger.info('hello');
logger.warn('hello');
await logger.flush();
expect(fetch).toHaveBeenCalledTimes(0);

logger = new Logger({}, null, false, 'frontend', 'warn');
logger.info('hello');
logger.debug('hello');
await logger.flush();
expect(fetch).toHaveBeenCalledTimes(0);

logger = new Logger({}, null, false, 'frontend', 'info');
logger.debug('hello');
await logger.flush();
expect(fetch).toHaveBeenCalledTimes(0);

logger = new Logger({}, null, false, 'frontend', 'error');
logger.warn('warn');
logger.error('error');
await logger.flush();
expect(fetch).toHaveBeenCalledTimes(1);

logger = new Logger({}, null, false, 'frontend', 'debug');
logger.warn('hello');
await logger.flush();
expect(fetch).toHaveBeenCalledTimes(2);
});
20 changes: 18 additions & 2 deletions src/logger.ts
Expand Up @@ -3,6 +3,7 @@ import { NetlifyInfo } from './platform/netlify';
import { isNoPrettyPrint, throttle } from './shared';

const url = config.getLogsEndpoint();
const LOG_LEVEL = process.env.AXIOM_LOG_LEVEL || 'debug';

export interface LogEvent {
level: string;
Expand All @@ -15,6 +16,13 @@ export interface LogEvent {
netlify?: NetlifyInfo;
}

export enum LogLevel {
debug = 0,
info = 1,
warn = 2,
error = 3,
}

export interface RequestReport {
startTime: number;
statusCode?: number;
Expand All @@ -38,13 +46,17 @@ export class Logger {
public logEvents: LogEvent[] = [];
throttledSendLogs = throttle(this.sendLogs, 1000);
children: Logger[] = [];
public logLevel: string;

constructor(
private args: { [key: string]: any } = {},
private req: RequestReport | null = null,
private autoFlush: Boolean = true,
public source: 'frontend' | 'lambda' | 'edge' = 'frontend'
) {}
public source: 'frontend' | 'lambda' | 'edge' = 'frontend',
logLevel?: string
) {
this.logLevel = logLevel || LOG_LEVEL || 'debug';
}

debug = (message: string, args: { [key: string]: any } = {}) => {
this._log('debug', message, args);
Expand All @@ -70,7 +82,11 @@ export class Logger {
};

_log = (level: string, message: string, args: { [key: string]: any } = {}) => {
if (LogLevel[level] < LogLevel[this.logLevel]) {
return;
}
const logEvent: LogEvent = { level, message, _time: new Date(Date.now()).toISOString(), fields: this.args || {} };

// check if passed args is an object, if its not an object, add it to fields.args
if (typeof args === 'object' && args !== null && Object.keys(args).length > 0) {
logEvent.fields = { ...logEvent.fields, ...args };
Expand Down

0 comments on commit f572a65

Please sign in to comment.