-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
618 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import _ from 'underscore'; | ||
import { | ||
ILogger, | ||
LogObject, | ||
LoggerConfig, | ||
LogLevel, | ||
DebugLogObject, | ||
} from './logging-types'; | ||
import levels from './levels'; | ||
import isDebugLevelOrMoreVerbose from './is-debug-level-or-more-verbose'; | ||
import { LambdaEventSourceType } from '../request-response-types'; | ||
|
||
export default class ConsoleLogger implements ILogger { | ||
|
||
protected _level: LogLevel; | ||
protected _interface: LambdaEventSourceType; | ||
protected _getTimeUntilFnTimeout: () => number; | ||
protected _fnStartTime: number; | ||
|
||
public constructor(config: LoggerConfig) { | ||
this._level = config.level || 'info'; | ||
this._interface = config.interface; | ||
this._fnStartTime = typeof config.fnStartTime === 'undefined' ? Date.now() : config.fnStartTime; | ||
this._getTimeUntilFnTimeout = config.getTimeUntilFnTimeout; | ||
} | ||
|
||
public trace(msg: string, data?: unknown): void { | ||
this._log('trace', msg, data); | ||
} | ||
|
||
public debug(msg: string, data?: unknown): void { | ||
this._log('debug', msg, data); | ||
} | ||
|
||
public info(msg: string, data?: unknown): void { | ||
this._log('info', msg, data); | ||
} | ||
|
||
public warn(msg: string, data?: unknown): void { | ||
this._log('warn', msg, data); | ||
} | ||
|
||
public error(msg: string, data?: unknown): void { | ||
this._log('error', msg, data); | ||
} | ||
|
||
public fatal(msg: string, data?: unknown): void { | ||
this._log('fatal', msg, data); | ||
} | ||
|
||
public getLevel(): LogLevel { | ||
return this._level; | ||
} | ||
|
||
public setLevel(level: LogLevel): void { | ||
this._level = level; | ||
} | ||
|
||
/** | ||
* Perform the actual message logging | ||
*/ | ||
protected _log(level: LogLevel, msg: string, data?: unknown): void { | ||
if (this._shouldLog(level)) { | ||
// eslint-disable-next-line no-console | ||
console.log(JSON.stringify(this._makeLogObject(level, msg, data))); | ||
} | ||
} | ||
|
||
/** | ||
* @returns `true` if the given level should be logged at this logger's current log | ||
* level setting. | ||
*/ | ||
protected _shouldLog(level: LogLevel): boolean { | ||
// Log if the level is higher priority than the current log level setting. | ||
// e.g. error (50) >= info (30) | ||
return levels[level] >= levels[this._level]; | ||
} | ||
|
||
/** | ||
* Creates an object to be logged | ||
*/ | ||
protected _makeLogObject(level: LogLevel, msg: string, data?: unknown): LogObject | DebugLogObject { | ||
let logLine: LogObject = { level, msg }; | ||
|
||
if (!_.isUndefined(data)) { | ||
logLine.data = data; | ||
} | ||
|
||
if (isDebugLevelOrMoreVerbose(level)) { | ||
let debugLogLine = logLine as DebugLogObject; | ||
|
||
debugLogLine.int = this._interface; | ||
debugLogLine.remaining = this._getTimeUntilFnTimeout(); | ||
debugLogLine.timer = this._getTimeSinceFnStart(); | ||
|
||
return debugLogLine; | ||
} | ||
|
||
return logLine; | ||
} | ||
|
||
/** | ||
* The approximate time, in milliseconds, since the Lambda function started executing. | ||
*/ | ||
protected _getTimeSinceFnStart(): number { | ||
return Date.now() - this._fnStartTime; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { LogLevel } from './logging-types'; | ||
import levels from './levels'; | ||
|
||
/** | ||
* @returns `true` if the given log level is `'debug'` or a more verbose level (e.g. | ||
* `'trace'`). | ||
*/ | ||
function isDebugLevelOrMoreVerbose(level: LogLevel): boolean { | ||
// More verbose levels have lower priority numbers | ||
return levels[level] <= levels.debug; | ||
} | ||
|
||
export default isDebugLevelOrMoreVerbose; |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { LogLevels } from './logging-types'; | ||
|
||
/** | ||
* A map of all available log levels. The key is the `LogLevelLabel` and the value is the | ||
* "priority". The logger's log level is set to one of these keys. When a message logging | ||
* function such as `debug` is called, it is only logged if the message's "priority" is | ||
* greater than or equal to the priority of the current log level. For example: | ||
* | ||
* ``` | ||
* const logger = new ConsoleLogger({ | ||
* level: 'info', | ||
* interface: 'ALB', | ||
* getTimeUntilFnTimeout: () => { return 0; } | ||
* }); | ||
* | ||
* // error (priority: 50) is >= info (priority: 30), so this message is logged | ||
* logger.error('error'); | ||
* | ||
* // debug (priority: 20) is NOT >= info (priority 30), so this message is NOT logged | ||
* logger.debug('debug'); | ||
* ``` | ||
* | ||
* Logging level priorities are for internal use and are not exposed on the public API. | ||
* Users of the public API adjust the logging level using the `LogLevel` strings. | ||
*/ | ||
const levels: LogLevels = { | ||
trace: 10, | ||
debug: 20, | ||
info: 30, | ||
warn: 40, | ||
error: 50, | ||
fatal: 60, | ||
silent: Number.MAX_SAFE_INTEGER, | ||
}; | ||
|
||
export default levels; |
Oops, something went wrong.