-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extended logging support (#603)
* feat: extended logging support * test: add logger tests * refactor(clouds): replace console with a logger * refactor: check log level value * chore: update examples * docs: add logger options
- Loading branch information
Showing
21 changed files
with
419 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Enable debug log | ||
DEBUG=uploadx:* | ||
LOG_LEVEL=debug | ||
|
||
PORT=3002 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,80 @@ | ||
import { debug } from 'debug'; | ||
import { formatWithOptions } from 'util'; | ||
|
||
type LoggerInstance = ((label?: any, ...data: any[]) => void) & { enabled?: boolean }; | ||
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none'; | ||
const levels = ['debug', 'info', 'warn', 'error', 'none']; | ||
|
||
export class Logger { | ||
static get(namespace: string): LoggerInstance { | ||
return debug('uploadx').extend(namespace.toLowerCase()); | ||
enum PriorityOf { | ||
debug, | ||
info, | ||
warn, | ||
error, | ||
none | ||
} | ||
|
||
export interface LoggerOptions { | ||
logger?: Logger; | ||
logLevel?: LogLevel; | ||
label?: string; | ||
write?: (data: unknown[], level?: LogLevel) => void; | ||
} | ||
|
||
export interface Logger { | ||
logLevel?: LogLevel; | ||
debug(...data: any[]): void; | ||
info(...data: any[]): void; | ||
warn(...data: any[]): void; | ||
error(...data: any[]): void; | ||
} | ||
|
||
/** | ||
* Basic logger implementation | ||
*/ | ||
export class BasicLogger implements Logger { | ||
label: string; | ||
private readonly logger: Logger; | ||
private _logLevel: LogLevel = 'none'; | ||
|
||
constructor(readonly options: LoggerOptions = {}) { | ||
this.logger = options.logger || console; | ||
this.label = options.label ?? 'uploadx:'; | ||
if (options.logLevel) this.logLevel = options.logLevel; | ||
if (options.write) this.write = options.write; | ||
} | ||
|
||
get logLevel(): LogLevel { | ||
return this._logLevel; | ||
} | ||
|
||
set logLevel(value: LogLevel) { | ||
if (value && !levels.includes(value)) { | ||
throw new Error(`Invalid log level: ${value}, supported levels are: ${levels.join(', ')}.`); | ||
} | ||
this._logLevel = value; | ||
} | ||
|
||
write = (data: unknown[], level: Exclude<LogLevel, 'none'>): void => { | ||
if (PriorityOf[level] >= PriorityOf[this._logLevel]) { | ||
const message = formatWithOptions({ colors: true, depth: 1, maxStringLength: 80 }, ...data); | ||
const timestamp = new Date().toISOString(); | ||
this.logger[level](`${timestamp} ${level.toUpperCase()} ${this.label} ${message}`); | ||
} | ||
}; | ||
|
||
info(...data: unknown[]): void { | ||
this.write(data, 'info'); | ||
} | ||
|
||
warn(...data: unknown[]): void { | ||
this.write(data, 'warn'); | ||
} | ||
|
||
error(...data: unknown[]): void { | ||
this.write(data, 'error'); | ||
} | ||
|
||
debug(...data: unknown[]): void { | ||
this.write(data, 'debug'); | ||
} | ||
} | ||
|
||
export const logger = new BasicLogger({}); |
Oops, something went wrong.