Skip to content

Commit

Permalink
Allow disabling logger scopes with debug flag (#2133)
Browse files Browse the repository at this point in the history
* Allow disabling logger scopes with debug flag

* Update changelogs
  • Loading branch information
stwiname authored Oct 31, 2023
1 parent c15b9b0 commit 6889408
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Missing dependencies for testing command

### Added
- Logger now supports negative filters. To use this prefix the logger name with a `-`. E.g `--debug="*,-SQL"` (#2133)

## [3.1.1] - 2023-10-25
### Fixed
- Update node-core with fix for crash when creating a dynamic datasource
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/yargs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export const yargsOptions = yargs(hideBin(process.argv))
},
debug: {
demandOption: false,
describe: `Enable debug logging for specific scopes, this will override log-level. "*" will enable debug everywhere, or comma separated strings for specific scopes. e.g. "SQL,dictionary"`,
describe: `Enable debug logging for specific scopes, this will override log-level. "*" will enable debug everywhere, or comma separated strings for specific scopes. e.g. "SQL,dictionary". To disable specific scopes you can prefix them with '-'. e.g. "*,-SQL"`,
type: 'string',
},
ipfs: {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Logger now supports negative filters. To use this prefix the logger name with a `-`. E.g `--debug="*,-SQL"` (#2133)

## [2.4.4] - 2023-10-11
### Added
Expand Down
23 changes: 19 additions & 4 deletions packages/utils/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {stringify} from 'flatted';
import Pino, {LevelWithSilent} from 'pino';
import {createStream} from 'rotating-file-stream';
import {colorizeLevel, ctx} from './colors';
import {LEVELS, LEVELS_MAP} from './constants';
export interface LoggerOption {
level?: string;
filepath?: string;
Expand All @@ -21,10 +22,10 @@ export interface LoggerOption {
export class Logger {
private pino: Pino.Logger;
private childLoggers: {[category: string]: Pino.Logger} = {};
private debugFilter: string[];
private _debugFilter: string[];

constructor({filepath, level: logLevel = 'info', nestedKey, outputFormat, rotate, debugFilter = []}: LoggerOption) {
this.debugFilter = debugFilter;
this._debugFilter = debugFilter;

const options = {
messageKey: 'message',
Expand Down Expand Up @@ -120,14 +121,28 @@ export class Logger {
}

setDebugFilter(debugFilter: string[]): void {
this.debugFilter = debugFilter;
this._debugFilter = debugFilter;
Object.keys(this.childLoggers).map((key) => this.applyChildDebug(key));
}

private get debugFilter(): string[] {
return this._debugFilter.map((f) => f.trim());
}

private applyChildDebug(category: string) {
if (this.debugFilter.includes(category) && this.childLoggers[category].level) {
if (!this.childLoggers[category].level) return;

if (this.debugFilter.includes(`-${category}`)) {
this.pino.info(`Debug logging is disabled for ${category}`);
// Set the log level to the global log level or INFO if its debug
const newLevel = Math.max(LEVELS_MAP[<LevelWithSilent>this.pino.level], LEVELS_MAP.info);
this.childLoggers[category].level = LEVELS[newLevel as keyof typeof LEVELS].toLowerCase();
} else if (this.debugFilter.includes(category)) {
this.pino.info(`Debug logging is enabled for ${category}`);
this.childLoggers[category].level = 'debug';
} else if (this.debugFilter.includes('*')) {
// Don't log wildcards, it spams the output
this.childLoggers[category].level = 'debug';
}
}
}

0 comments on commit 6889408

Please sign in to comment.