Skip to content

Commit

Permalink
feat: add log level filtering to transports
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielelpidio committed Jan 30, 2025
1 parent e35921a commit dd991ac
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/logging/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Logger {
this.config = { ...initConfig };
}

raw(log: LogEvent) {
raw(log: any) {
this.config.transports.forEach((transport) => transport.log([log]));
}
debug = (message: string, args: { [key: string]: any } = {}) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/logging/src/transports/axiom-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Transport } from '.';
import { LogLevel } from '../logger';
import { Transport } from './transport';
import { SimpleFetchTransport } from './fetch';
interface AxiomFetchConfig {
dataset: string;
token: string;
url?: string;
autoFlush?: boolean | number;
logLevel?: LogLevel;
}

const DEFAULT_URL = 'https://api.axiom.co';
Expand All @@ -17,6 +19,7 @@ export class AxiomFetchTransport extends SimpleFetchTransport implements Transpo
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${config.token}` },
},
autoFlush: config.autoFlush,
logLevel: config.logLevel,
});
}
}
41 changes: 32 additions & 9 deletions packages/logging/src/transports/axiom-js.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
import type { Axiom } from "@axiomhq/js";
import { Axiom, AxiomWithoutBatching } from '@axiomhq/js';
import { LogLevel } from '../logger';
import { Transport } from './transport';

export class AxiomJSTransport {
private axiom: Axiom;
private dataset: string;
interface AxiomJSTransportConfig {
axiom: Axiom | AxiomWithoutBatching;
dataset: string;
logLevel?: LogLevel;
}
export class AxiomJSTransport implements Transport {
private config: AxiomJSTransportConfig;
private promises: Promise<any>[] = [];

constructor(axiom: Axiom, dataset: string) {
this.axiom = axiom;
this.dataset = dataset;
constructor(config: AxiomJSTransportConfig) {
this.config = config;
}

log(logs: any[]) {
this.axiom.ingest(this.dataset, logs);
const filteredLogs = logs.filter(
(log) =>
(LogLevel[log.level as keyof typeof LogLevel] ?? LogLevel.info) >= (this.config.logLevel ?? LogLevel.info),
);

if (filteredLogs.length === 0) {
return;
}

if (this.config.axiom instanceof Axiom) {
this.config.axiom.ingest(this.config.dataset, filteredLogs);
} else if (this.config.axiom instanceof AxiomWithoutBatching) {
this.promises.push(this.config.axiom.ingest(this.config.dataset, filteredLogs));
}
}

async flush() {
await this.axiom.flush();
if (this.config.axiom instanceof Axiom) {
await this.config.axiom.flush();
} else {
await Promise.allSettled(this.promises);
}
}
}
11 changes: 8 additions & 3 deletions packages/logging/src/transports/console.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Transport } from '.';
import { LogEvent } from '..';
import { Transport } from './transport';
import { LogEvent, LogLevel } from '..';
import { isBrowser } from '../runtime';
export interface ConsoleTransportConfig {
prettyPrint?: boolean;
logLevel?: LogLevel;
}

const levelColors: { [key: string]: any } = {
Expand All @@ -26,12 +27,16 @@ const levelColors: { [key: string]: any } = {

export class ConsoleTransport implements Transport {
private config: ConsoleTransportConfig;

constructor(config?: ConsoleTransportConfig) {
this.config = { ...config };
}

log: Transport['log'] = (logs) => {
logs.forEach((log) => {
this.prettyPrint(log);
if (LogLevel[log.level as keyof typeof LogLevel] >= (this.config.logLevel ?? LogLevel.info)) {
this.prettyPrint(log);
}
});
};

Expand Down
12 changes: 9 additions & 3 deletions packages/logging/src/transports/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Transport } from '.';
import { LogEvent } from '..';
import { Transport } from './transport';
import { LogEvent, LogLevel } from '..';

interface FetchConfig {
input: Parameters<typeof fetch>[0];
init?: Omit<Parameters<typeof fetch>[1], 'body'>;
autoFlush?: number | boolean;
logLevel?: LogLevel;
}

export class SimpleFetchTransport implements Transport {
Expand All @@ -17,7 +18,12 @@ export class SimpleFetchTransport implements Transport {
}

log: Transport['log'] = (logs) => {
this.events.push(...logs);
const filteredLogs = logs.filter(
(log) =>
(LogLevel[log.level as keyof typeof LogLevel] ?? LogLevel.info) >= (this.fetchConfig.logLevel ?? LogLevel.info),
);

this.events.push(...filteredLogs);

if (typeof this.fetchConfig.autoFlush === 'undefined' || this.fetchConfig.autoFlush === false) {
return;
Expand Down
8 changes: 1 addition & 7 deletions packages/logging/src/transports/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { LogEvent } from '..';

export interface Transport {
log: (logs: LogEvent[]) => Promise<void> | void;
flush: () => Promise<void> | void;
}

export * from './transport';
export * from './axiom-js';
export * from './axiom-fetch';
export * from './console';
Expand Down
5 changes: 4 additions & 1 deletion packages/logging/src/transports/proxy-transport.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Transport } from '.';
import { LogLevel } from '../logger';
import { Transport } from './transport';
import { SimpleFetchTransport } from './fetch';
interface AxiomProxyConfig {
url: string;
autoFlush?: boolean | number;
logLevel?: LogLevel;
}
export class AxiomProxyTransport extends SimpleFetchTransport implements Transport {
constructor(config: AxiomProxyConfig) {
super({
input: config.url,
autoFlush: config.autoFlush,
logLevel: config.logLevel,
});
}
}
4 changes: 4 additions & 0 deletions packages/logging/src/transports/transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Transport {
log: (logs: any[]) => Promise<void> | void;
flush: () => Promise<void> | void;
}

0 comments on commit dd991ac

Please sign in to comment.