@spot-meetings/backend-logger
Advanced tools
Comparing version 2.0.1 to 2.2.0
@@ -14,2 +14,8 @@ import winston from 'winston'; | ||
} | ||
export declare enum LogOutput { | ||
Summary = "summary", | ||
Details = "details", | ||
Raw = "raw" | ||
} | ||
export declare const logIcons: Map<LogLevel, string>; | ||
export interface LogReporter { | ||
@@ -16,0 +22,0 @@ readonly version: string; |
@@ -6,5 +6,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createLogger = exports.LogLevel = void 0; | ||
exports.createLogger = exports.logIcons = exports.LogOutput = exports.LogLevel = void 0; | ||
/* eslint-disable no-bitwise */ | ||
const date_fns_1 = require("date-fns"); | ||
const json_colorizer_1 = __importDefault(require("json-colorizer")); | ||
const winston_1 = __importDefault(require("winston")); | ||
const { timestamp, splat, json, errors, combine, } = winston_1.default.format; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const { timestamp, splat, json, errors, combine } = winston_1.default.format; | ||
/** | ||
@@ -23,2 +27,24 @@ * @see https://github.com/winstonjs/winston#logging | ||
})(LogLevel = exports.LogLevel || (exports.LogLevel = {})); | ||
var LogOutput; | ||
(function (LogOutput) { | ||
LogOutput["Summary"] = "summary"; | ||
LogOutput["Details"] = "details"; | ||
LogOutput["Raw"] = "raw"; | ||
})(LogOutput = exports.LogOutput || (exports.LogOutput = {})); | ||
exports.logIcons = new Map([ | ||
[LogLevel.Error, '💥'], | ||
[LogLevel.Warn, '🛑'], | ||
[LogLevel.Info, '🔔'], | ||
[LogLevel.Http, '🚛'], | ||
[LogLevel.Verbose, '💬'], | ||
[LogLevel.Debug, '🐛'], | ||
[LogLevel.Silly, '🤪'], | ||
]); | ||
const colors = { | ||
STRING_LITERAL: 'white', | ||
BOOLEAN_LITERAL: 'blue', | ||
NUMBER_LITERAL: 'cyan', | ||
NULL_LITERAL: 'magenta', | ||
STRING_KEY: 'white.dim', | ||
}; | ||
/** | ||
@@ -28,11 +54,35 @@ * SpotLogger Class | ||
const createLogger = (id, ip, version) => { | ||
let logLevel = process.env.LOG_LEVEL; | ||
if (!Object.values(LogLevel).includes(process.env.LOG_LEVEL)) { | ||
let outputType = process.env.LOG_OUTPUT || LogOutput.Raw; | ||
if (!Object.values(LogOutput).includes(outputType)) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`The provided LOG_LEVEL "${process.env.LOG_LEVEL}" is not supported. Defaulting to "info".`); | ||
logLevel = 'info'; | ||
console.warn(`The provided LOG_OUTPUT "${outputType}" is not supported. Defaulting to "${LogOutput.Raw}".`); | ||
outputType = LogOutput.Raw; | ||
} | ||
let logLevel = process.env.LOG_LEVEL || LogLevel.Info; | ||
if (!Object.values(LogLevel).includes(logLevel)) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`The provided LOG_LEVEL "${logLevel}" is not supported. Defaulting to "${LogLevel.Info}".`); | ||
logLevel = LogLevel.Info; | ||
} | ||
const formats = [errors({ stack: true }), timestamp(), splat(), json()]; | ||
// If any of the formatted output flags are set we pretty print. | ||
if ([LogOutput.Details, LogOutput.Summary].includes(outputType)) { | ||
formats.push(winston_1.default.format.printf((info) => { | ||
const { level, message, timestamp: ts } = info; | ||
const label = exports.logIcons.get(level) || '❔'; | ||
const time = chalk_1.default.yellow((0, date_fns_1.format)(new Date(ts), '[HH:mm:ss.SSS]')); | ||
const header = chalk_1.default.bold(`${time} ${label} ${message}`); | ||
const parts = [header]; | ||
if (outputType === LogOutput.Details) { | ||
parts.push((0, json_colorizer_1.default)(JSON.stringify(info), { | ||
pretty: true, | ||
colors, | ||
}).replace(/\\n/g, '\n')); | ||
} | ||
return parts.join('\n'); | ||
})); | ||
} | ||
return winston_1.default.createLogger({ | ||
format: combine(errors({ stack: true }), timestamp(), splat(), json()), | ||
transports: [new winston_1.default.transports.Console()], | ||
format: combine(...formats), | ||
level: logLevel, | ||
@@ -39,0 +89,0 @@ defaultMeta: { |
{ | ||
"name": "@spot-meetings/backend-logger", | ||
"version": "2.0.1", | ||
"version": "2.2.0", | ||
"description": "Spot's backend logger module.", | ||
@@ -32,2 +32,5 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"chalk": "^4.1.2", | ||
"date-fns": "^2.24.0", | ||
"json-colorizer": "^2.2.2", | ||
"winston": "^3.3.3" | ||
@@ -34,0 +37,0 @@ }, |
@@ -21,2 +21,12 @@ # @spot-meetings/backend-logger | ||
As this module is just a pre-configured instance of Winston, please refer to their [usage documentation](https://github.com/winstonjs/winston#usage) for more details. | ||
### Log Output | ||
To set different output types, set the `LOG_OUTPUT` env var to any of the following: | ||
- `summary`: Just the log message with a timestamp and a label. | ||
- `details`: The above plus detailed information about the log message in JSON format, pretty printed. | ||
- `raw`: The raw, inline JSON message. | ||
### Example | ||
@@ -23,0 +33,0 @@ |
@@ -0,6 +1,8 @@ | ||
/* eslint-disable no-bitwise */ | ||
import { format as formatDate } from 'date-fns' | ||
import colorizer from 'json-colorizer' | ||
import winston from 'winston' | ||
import chalk from 'chalk' | ||
const { | ||
timestamp, splat, json, errors, combine, | ||
} = winston.format | ||
const { timestamp, splat, json, errors, combine } = winston.format | ||
@@ -20,2 +22,18 @@ /** | ||
export enum LogOutput { | ||
Summary = 'summary', | ||
Details = 'details', | ||
Raw = 'raw', | ||
} | ||
export const logIcons = new Map<LogLevel, string>([ | ||
[LogLevel.Error, '💥'], | ||
[LogLevel.Warn, '🛑'], | ||
[LogLevel.Info, '🔔'], | ||
[LogLevel.Http, '🚛'], | ||
[LogLevel.Verbose, '💬'], | ||
[LogLevel.Debug, '🐛'], | ||
[LogLevel.Silly, '🤪'], | ||
]) | ||
export interface LogReporter { | ||
@@ -39,2 +57,10 @@ readonly version: string | ||
const colors = { | ||
STRING_LITERAL: 'white', | ||
BOOLEAN_LITERAL: 'blue', | ||
NUMBER_LITERAL: 'cyan', | ||
NULL_LITERAL: 'magenta', | ||
STRING_KEY: 'white.dim', | ||
} | ||
/** | ||
@@ -44,21 +70,53 @@ * SpotLogger Class | ||
export const createLogger = (id: string, ip: string, version: string) => { | ||
let logLevel = process.env.LOG_LEVEL | ||
let outputType = (process.env.LOG_OUTPUT as LogOutput) || LogOutput.Raw | ||
if (!Object.values(LogLevel).includes(process.env.LOG_LEVEL as LogLevel)) { | ||
if (!Object.values(LogOutput).includes(outputType)) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`The provided LOG_LEVEL "${process.env.LOG_LEVEL}" is not supported. Defaulting to "info".`, | ||
`The provided LOG_OUTPUT "${outputType}" is not supported. Defaulting to "${LogOutput.Raw}".`, | ||
) | ||
logLevel = 'info' | ||
outputType = LogOutput.Raw | ||
} | ||
let logLevel: LogLevel = (process.env.LOG_LEVEL as LogLevel) || LogLevel.Info | ||
if (!Object.values(LogLevel).includes(logLevel)) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`The provided LOG_LEVEL "${logLevel}" is not supported. Defaulting to "${LogLevel.Info}".`, | ||
) | ||
logLevel = LogLevel.Info | ||
} | ||
const formats = [errors({ stack: true }), timestamp(), splat(), json()] | ||
// If any of the formatted output flags are set we pretty print. | ||
if ([LogOutput.Details, LogOutput.Summary].includes(outputType)) { | ||
formats.push( | ||
winston.format.printf((info) => { | ||
const { level, message, timestamp: ts } = info | ||
const label = logIcons.get(level as LogLevel) || '❔' | ||
const time = chalk.yellow(formatDate(new Date(ts), '[HH:mm:ss.SSS]')) | ||
const header = chalk.bold(`${time} ${label} ${message}`) | ||
const parts = [header] | ||
if (outputType === LogOutput.Details) { | ||
parts.push( | ||
colorizer(JSON.stringify(info), { | ||
pretty: true, | ||
colors, | ||
}).replace(/\\n/g, '\n'), // To avoid having "\n" in the stack log | ||
) | ||
} | ||
return parts.join('\n') | ||
}), | ||
) | ||
} | ||
return winston.createLogger({ | ||
format: combine( | ||
errors({ stack: true }), | ||
timestamp(), | ||
splat(), | ||
json(), | ||
), | ||
transports: [new winston.transports.Console()], | ||
format: combine(...formats), | ||
level: logLevel, | ||
@@ -65,0 +123,0 @@ defaultMeta: { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14116
262
93
3
4
+ Addedchalk@^4.1.2
+ Addeddate-fns@^2.24.0
+ Addedjson-colorizer@^2.2.2
+ Added@babel/runtime@7.26.0(transitive)
+ Addedansi-styles@3.2.14.3.0(transitive)
+ Addedchalk@2.4.24.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddate-fns@2.30.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-flag@3.0.04.0.0(transitive)
+ Addedjson-colorizer@2.2.2(transitive)
+ Addedlodash.get@4.4.2(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedsupports-color@5.5.07.2.0(transitive)