@arashi/logger
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -7,4 +7,12 @@ ### Changelog | ||
#### [v3.0.0](https://github.com/arashijs/logger/compare/v2.1.0...v3.0.0) | ||
- Added ConsoleLogger [`9bf4ab6`](https://github.com/arashijs/logger/commit/9bf4ab6f1c0e73e06cb741d36b0f4daa1863bdc8) | ||
- deproll [`c62e6cf`](https://github.com/arashijs/logger/commit/c62e6cf21cb3d5f5200aed27073dbd21796c2acc) | ||
- deproll [`3aecbb1`](https://github.com/arashijs/logger/commit/3aecbb10f9b911005d609f4ee022c38d52f66522) | ||
#### [v2.1.0](https://github.com/arashijs/logger/compare/v2.0.1...v2.1.0) | ||
> 29 October 2021 | ||
- eslint upgrade [`725c4ee`](https://github.com/arashijs/logger/commit/725c4ee022cae967685508795a7c49c447dc81d6) | ||
@@ -11,0 +19,0 @@ - deproll [`d617ac0`](https://github.com/arashijs/logger/commit/d617ac09d7708cf2fd53212fa4b3254c47d82102) |
@@ -1,2 +0,2 @@ | ||
[@arashi/logger - v2.1.0](../README.md) / [Exports](../modules.md) / [Logger](../modules/Logger.md) / Logger | ||
[@arashi/logger - v3.0.0](../README.md) / [Exports](../modules.md) / [Logger](../modules/Logger.md) / Logger | ||
@@ -13,2 +13,8 @@ # Class: Logger | ||
↳↳ [`ConsoleLogger`](ConsoleLogger.ConsoleLogger-1.md) | ||
## Implements | ||
- `ILogger` | ||
## Table of contents | ||
@@ -31,2 +37,3 @@ | ||
- [\_getDefaultLogFilters](Logger.Logger-1.md#_getdefaultlogfilters) | ||
- [\_log](Logger.Logger-1.md#_log) | ||
- [\_shouldFilter](Logger.Logger-1.md#_shouldfilter) | ||
@@ -43,3 +50,2 @@ - [addFilter](Logger.Logger-1.md#addfilter) | ||
- [getLogLevel](Logger.Logger-1.md#getloglevel) | ||
- [getMaxFileSize](Logger.Logger-1.md#getmaxfilesize) | ||
- [getMaxListeners](Logger.Logger-1.md#getmaxlisteners) | ||
@@ -75,3 +81,3 @@ - [info](Logger.Logger-1.md#info) | ||
• **new Logger**(`serviceName?`, `logLevel?`, `logLocation?`) | ||
• **new Logger**(`serviceName?`, `logLevel?`) | ||
@@ -83,4 +89,3 @@ #### Parameters | ||
| `serviceName` | `string` | `'Generic'` | | ||
| `logLevel` | [`LogLevel`](../enums/LogLevel.LogLevel-1.md) | `undefined` | | ||
| `logLocation?` | `string` | `undefined` | | ||
| `logLevel` | `LogLevel` | `undefined` | | ||
@@ -93,3 +98,3 @@ #### Overrides | ||
[src/Logger.ts:36](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L36) | ||
[src/Logger.ts:46](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L46) | ||
@@ -100,3 +105,3 @@ ## Properties | ||
▪ `Static` `Readonly` **captureRejectionSymbol**: typeof [`captureRejectionSymbol`](Logger.Logger-1.md#capturerejectionsymbol) | ||
▪ `Static` `Readonly` **captureRejectionSymbol**: typeof [`captureRejectionSymbol`](ConsoleLogger.ConsoleLogger-1.md#capturerejectionsymbol) | ||
@@ -145,3 +150,3 @@ #### Inherited from | ||
▪ `Static` `Readonly` **errorMonitor**: typeof [`errorMonitor`](Logger.Logger-1.md#errormonitor) | ||
▪ `Static` `Readonly` **errorMonitor**: typeof [`errorMonitor`](ConsoleLogger.ConsoleLogger-1.md#errormonitor) | ||
@@ -182,3 +187,3 @@ This symbol shall be used to install a listener for only monitoring `'error'` | ||
[src/Logger.ts:170](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L170) | ||
[src/Logger.ts:179](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L179) | ||
@@ -197,6 +202,28 @@ ___ | ||
[src/Logger.ts:166](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L166) | ||
[src/Logger.ts:175](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L175) | ||
___ | ||
### \_log | ||
▸ `Protected` `Abstract` **_log**(`logLevel`, `message`, `metadata`): `void` | ||
#### Parameters | ||
| Name | Type | | ||
| :------ | :------ | | ||
| `logLevel` | `LogLevel` | | ||
| `message` | `string` | | ||
| `metadata` | [`ILogMetadata`](../interfaces/Logger.ILogMetadata.md) | | ||
#### Returns | ||
`void` | ||
#### Defined in | ||
[src/Logger.ts:215](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L215) | ||
___ | ||
### \_shouldFilter | ||
@@ -218,3 +245,3 @@ | ||
[src/Logger.ts:175](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L175) | ||
[src/Logger.ts:184](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L184) | ||
@@ -237,5 +264,9 @@ ___ | ||
#### Implementation of | ||
ILogger.addFilter | ||
#### Defined in | ||
[src/Logger.ts:142](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L142) | ||
[src/Logger.ts:151](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L151) | ||
@@ -289,5 +320,9 @@ ___ | ||
#### Implementation of | ||
ILogger.debug | ||
#### Defined in | ||
[src/Logger.ts:237](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L237) | ||
[src/Logger.ts:257](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L257) | ||
@@ -312,5 +347,9 @@ ___ | ||
#### Implementation of | ||
ILogger.deprecate | ||
#### Defined in | ||
[src/Logger.ts:261](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L261) | ||
[src/Logger.ts:281](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L281) | ||
@@ -336,5 +375,9 @@ ___ | ||
#### Implementation of | ||
ILogger.deprecateParameterType | ||
#### Defined in | ||
[src/Logger.ts:281](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L281) | ||
[src/Logger.ts:301](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L301) | ||
@@ -424,5 +467,9 @@ ___ | ||
#### Implementation of | ||
ILogger.error | ||
#### Defined in | ||
[src/Logger.ts:257](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L257) | ||
[src/Logger.ts:277](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L277) | ||
@@ -475,5 +522,9 @@ ___ | ||
#### Implementation of | ||
ILogger.getFilters | ||
#### Defined in | ||
[src/Logger.ts:162](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L162) | ||
[src/Logger.ts:171](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L171) | ||
@@ -484,25 +535,15 @@ ___ | ||
▸ **getLogLevel**(): [`LogLevel`](../enums/LogLevel.LogLevel-1.md) | ||
▸ **getLogLevel**(): `LogLevel` | ||
#### Returns | ||
[`LogLevel`](../enums/LogLevel.LogLevel-1.md) | ||
`LogLevel` | ||
#### Defined in | ||
#### Implementation of | ||
[src/Logger.ts:138](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L138) | ||
ILogger.getLogLevel | ||
___ | ||
### getMaxFileSize | ||
▸ **getMaxFileSize**(): `number` | ||
#### Returns | ||
`number` | ||
#### Defined in | ||
[src/Logger.ts:130](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L130) | ||
[src/Logger.ts:147](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L147) | ||
@@ -550,5 +591,9 @@ ___ | ||
#### Implementation of | ||
ILogger.info | ||
#### Defined in | ||
[src/Logger.ts:247](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L247) | ||
[src/Logger.ts:267](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L267) | ||
@@ -629,3 +674,3 @@ ___ | ||
| :------ | :------ | | ||
| `level` | [`LogLevel`](../enums/LogLevel.LogLevel-1.md) | | ||
| `level` | `LogLevel` | | ||
| `component` | `string` | | ||
@@ -639,5 +684,9 @@ | `message` | `any` | | ||
#### Implementation of | ||
ILogger.log | ||
#### Defined in | ||
[src/Logger.ts:207](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L207) | ||
[src/Logger.ts:218](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L218) | ||
@@ -958,5 +1007,9 @@ ___ | ||
#### Implementation of | ||
ILogger.removeFilter | ||
#### Defined in | ||
[src/Logger.ts:146](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L146) | ||
[src/Logger.ts:155](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L155) | ||
@@ -1083,5 +1136,9 @@ ___ | ||
#### Implementation of | ||
ILogger.setFilters | ||
#### Defined in | ||
[src/Logger.ts:153](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L153) | ||
[src/Logger.ts:162](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L162) | ||
@@ -1098,3 +1155,3 @@ ___ | ||
| :------ | :------ | | ||
| `level` | [`LogLevel`](../enums/LogLevel.LogLevel-1.md) | | ||
| `level` | `LogLevel` | | ||
@@ -1105,5 +1162,9 @@ #### Returns | ||
#### Implementation of | ||
ILogger.setLogLevel | ||
#### Defined in | ||
[src/Logger.ts:134](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L134) | ||
[src/Logger.ts:143](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L143) | ||
@@ -1161,5 +1222,9 @@ ___ | ||
#### Implementation of | ||
ILogger.silly | ||
#### Defined in | ||
[src/Logger.ts:232](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L232) | ||
[src/Logger.ts:252](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L252) | ||
@@ -1186,5 +1251,9 @@ ___ | ||
#### Implementation of | ||
ILogger.trace | ||
#### Defined in | ||
[src/Logger.ts:227](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L227) | ||
[src/Logger.ts:247](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L247) | ||
@@ -1209,5 +1278,9 @@ ___ | ||
#### Implementation of | ||
ILogger.verbose | ||
#### Defined in | ||
[src/Logger.ts:242](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L242) | ||
[src/Logger.ts:262](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L262) | ||
@@ -1232,5 +1305,9 @@ ___ | ||
#### Implementation of | ||
ILogger.warn | ||
#### Defined in | ||
[src/Logger.ts:252](https://github.com/arashijs/logger/blob/c50e05b/src/Logger.ts#L252) | ||
[src/Logger.ts:272](https://github.com/arashijs/logger/blob/9bf4ab6/src/Logger.ts#L272) | ||
@@ -1237,0 +1314,0 @@ ___ |
@@ -1,4 +0,4 @@ | ||
[@arashi/logger - v2.1.0](README.md) / Exports | ||
[@arashi/logger - v3.0.0](README.md) / Exports | ||
# @arashi/logger - v2.1.0 | ||
# @arashi/logger - v3.0.0 | ||
@@ -9,6 +9,4 @@ ## Table of contents | ||
- [ILogEvent](modules/ILogEvent.md) | ||
- [LogEvent](modules/LogEvent.md) | ||
- [LogLevel](modules/LogLevel.md) | ||
- [ConsoleLogger](modules/ConsoleLogger.md) | ||
- [Logger](modules/Logger.md) | ||
- [api](modules/api.md) |
@@ -1,2 +0,2 @@ | ||
[@arashi/logger - v2.1.0](../README.md) / [Exports](../modules.md) / api | ||
[@arashi/logger - v3.0.0](../README.md) / [Exports](../modules.md) / api | ||
@@ -9,5 +9,4 @@ # Module: api | ||
- [ILogEvent](api.md#ilogevent) | ||
- [LogEvent](api.md#logevent) | ||
- [LogLevel](api.md#loglevel) | ||
- [ConsoleLogger](api.md#consolelogger) | ||
- [ILogMetadata](api.md#ilogmetadata) | ||
- [Logger](api.md#logger) | ||
@@ -17,22 +16,16 @@ | ||
### ILogEvent | ||
### ConsoleLogger | ||
Re-exports: [ILogEvent](../interfaces/ILogEvent.ILogEvent-1.md) | ||
Re-exports: [ConsoleLogger](../classes/ConsoleLogger.ConsoleLogger-1.md) | ||
___ | ||
### LogEvent | ||
### ILogMetadata | ||
Re-exports: [LogEvent](../enums/LogEvent.LogEvent-1.md) | ||
Re-exports: [ILogMetadata](../interfaces/Logger.ILogMetadata.md) | ||
___ | ||
### LogLevel | ||
Re-exports: [LogLevel](../enums/LogLevel.LogLevel-1.md) | ||
___ | ||
### Logger | ||
Re-exports: [Logger](../classes/Logger.Logger-1.md) |
@@ -1,2 +0,2 @@ | ||
[@arashi/logger - v2.1.0](../README.md) / [Exports](../modules.md) / Logger | ||
[@arashi/logger - v3.0.0](../README.md) / [Exports](../modules.md) / Logger | ||
@@ -10,1 +10,5 @@ # Module: Logger | ||
- [Logger](../classes/Logger.Logger-1.md) | ||
### Interfaces | ||
- [ILogMetadata](../interfaces/Logger.ILogMetadata.md) |
@@ -1,3 +0,3 @@ | ||
@arashi/logger - v2.1.0 / [Exports](modules.md) | ||
@arashi/logger - v3.0.0 / [Exports](modules.md) | ||
# logger |
@@ -1,4 +0,2 @@ | ||
export { ILogEvent } from './ILogEvent'; | ||
export { LogEvent } from './LogEvent'; | ||
export { Logger } from './Logger'; | ||
export { LogLevel } from './LogLevel'; | ||
export { Logger, ILogMetadata } from './Logger'; | ||
export { ConsoleLogger } from './ConsoleLogger'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LogLevel = exports.Logger = exports.LogEvent = void 0; | ||
var LogEvent_1 = require("./LogEvent"); | ||
Object.defineProperty(exports, "LogEvent", { enumerable: true, get: function () { return LogEvent_1.LogEvent; } }); | ||
exports.ConsoleLogger = exports.Logger = void 0; | ||
var Logger_1 = require("./Logger"); | ||
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return Logger_1.Logger; } }); | ||
var LogLevel_1 = require("./LogLevel"); | ||
Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return LogLevel_1.LogLevel; } }); | ||
var ConsoleLogger_1 = require("./ConsoleLogger"); | ||
Object.defineProperty(exports, "ConsoleLogger", { enumerable: true, get: function () { return ConsoleLogger_1.ConsoleLogger; } }); | ||
//# sourceMappingURL=api.js.map |
/// <reference types="node" /> | ||
import { LogLevel } from './LogLevel'; | ||
import { LogLevel, ILogger } from '@arashi/interfaces'; | ||
import { EventEmitter } from 'events'; | ||
export declare class Logger extends EventEmitter { | ||
export interface ILogMetadata { | ||
service: string; | ||
component: string; | ||
meta: Record<any, any>; | ||
} | ||
export declare abstract class Logger extends EventEmitter implements ILogger { | ||
private $filters; | ||
private $logger; | ||
private $logLocation; | ||
private $logLevel; | ||
private $serviceName; | ||
constructor(serviceName?: string, logLevel?: LogLevel, logLocation?: string); | ||
getMaxFileSize(): number; | ||
constructor(serviceName?: string, logLevel?: LogLevel); | ||
setLogLevel(level: LogLevel): void; | ||
@@ -21,2 +24,3 @@ getLogLevel(): LogLevel; | ||
private $parseMessage; | ||
protected abstract _log(logLevel: LogLevel, message: string, metadata: ILogMetadata): void; | ||
log(level: LogLevel, component: string, message: any, metadata?: Record<any, any>): void; | ||
@@ -23,0 +27,0 @@ trace(component: string, message: any, metadata?: Record<any, any>): void; |
@@ -5,86 +5,16 @@ "use strict"; | ||
const utils = require("util"); | ||
const Winston = require("winston"); | ||
const LogLevel_1 = require("./LogLevel"); | ||
const interfaces_1 = require("@arashi/interfaces"); | ||
const events_1 = require("events"); | ||
const LogEvent_1 = require("./LogEvent"); | ||
const Path = require("path"); | ||
const F_RESET = '\x1b[0m'; | ||
const F_DIM = '\x1b[2m'; | ||
const F_FG_BLUE = '\x1b[34m'; | ||
const F_FG_CYAN = '\x1b[36m'; | ||
const DEFAULT_MAX_FILE_SIZE = 52428800; | ||
class Logger extends events_1.EventEmitter { | ||
constructor(serviceName = 'Generic', logLevel = LogLevel_1.LogLevel.INFO, logLocation) { | ||
constructor(serviceName = 'Generic', logLevel = interfaces_1.LogLevel.INFO) { | ||
super(); | ||
this.$logLocation = logLocation ? Path.resolve(logLocation) : null; | ||
this.$filters = this._getDefaultLogFilters(); | ||
this.$serviceName = serviceName; | ||
let format = Winston.format((info, opts) => { | ||
const MESSAGE = Symbol.for('message'); | ||
info[MESSAGE] = `${F_DIM}${info.timestamp}${F_RESET} - [${F_FG_BLUE}${info.service}${F_RESET}][${F_FG_CYAN}${info.component}${F_RESET}]: ${info.level}: ${info.message}`; | ||
return info; | ||
})(); | ||
let consoleTransport = new Winston.transports.Console({ | ||
format: Winston.format.combine(Winston.format.colorize(), format, Winston.format.errors({ stack: true })) | ||
}); | ||
consoleTransport.on('logged', (log) => { | ||
this.emit(LogEvent_1.LogEvent.LOG, log.message); | ||
}); | ||
let transports = [consoleTransport]; | ||
if (this.$logLocation) { | ||
transports.push(new Winston.transports.File({ | ||
filename: Path.resolve(this.$logLocation, `${serviceName}.json.log`), | ||
level: logLevel, | ||
maxsize: this.getMaxFileSize(), | ||
format: Winston.format.combine(Winston.format.json(), Winston.format.errors({ stack: true }), Winston.format((info, opts) => { | ||
const MESSAGE = Symbol.for('message'); | ||
info[MESSAGE] = utils.inspect({ | ||
level: info.level, | ||
message: info.message, | ||
timestamp: info.timestamp, | ||
service: info.service, | ||
component: info.component, | ||
meta: info.meta | ||
}, { | ||
depth: Infinity, | ||
colors: false, | ||
maxArrayLength: Infinity, | ||
showProxy: true, | ||
breakLength: Infinity | ||
}); | ||
return info; | ||
})()) | ||
})); | ||
transports.push(new Winston.transports.File({ | ||
filename: Path.resolve(this.$logLocation, `${serviceName}.log`), | ||
maxsize: this.getMaxFileSize(), | ||
level: logLevel, | ||
format: Winston.format.combine(Winston.format.simple(), Winston.format.errors({ stack: true }), format) | ||
})); | ||
transports.push(new Winston.transports.File({ | ||
filename: Path.resolve(this.$logLocation, `${serviceName}.errors.log`), | ||
maxsize: this.getMaxFileSize(), | ||
level: LogLevel_1.LogLevel.WARN, | ||
format: Winston.format.combine(Winston.format.simple(), Winston.format.errors({ stack: true }), format) | ||
})); | ||
} | ||
this.$logger = Winston.createLogger({ | ||
level: logLevel, | ||
format: Winston.format.combine(Winston.format.timestamp({ | ||
format: 'YYYY-MM-DD HH:mm:ss' | ||
}), Winston.format.errors({ stack: true })), | ||
defaultMeta: { | ||
service: serviceName | ||
}, | ||
transports: transports | ||
}); | ||
this.$logLevel = logLevel; | ||
} | ||
getMaxFileSize() { | ||
return DEFAULT_MAX_FILE_SIZE; | ||
} | ||
setLogLevel(level) { | ||
this.$logger.level = level; | ||
this.$logLevel = level; | ||
} | ||
getLogLevel() { | ||
return this.$logger.level; | ||
return this.$logLevel; | ||
} | ||
@@ -148,3 +78,4 @@ addFilter(reg) { | ||
if (this._shouldFilter(message)) { | ||
this.$logger.log(level, this.$parseMessage(message), { | ||
let msg = this.$parseMessage(message); | ||
this._log(level, msg, { | ||
service: this.$serviceName, | ||
@@ -154,2 +85,9 @@ component: component, | ||
}); | ||
this.emit(interfaces_1.LogEvent.LOG, { | ||
level: level, | ||
message: msg, | ||
service: this.$serviceName, | ||
component: component, | ||
meta: metadata | ||
}); | ||
} | ||
@@ -161,18 +99,18 @@ } | ||
silly(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.SILLY, component, message, metadata); | ||
this.log(interfaces_1.LogLevel.SILLY, component, message, metadata); | ||
} | ||
debug(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.DEBUG, component, message, metadata); | ||
this.log(interfaces_1.LogLevel.DEBUG, component, message, metadata); | ||
} | ||
verbose(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.VERBOSE, component, message, metadata); | ||
this.log(interfaces_1.LogLevel.VERBOSE, component, message, metadata); | ||
} | ||
info(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.INFO, component, message, metadata); | ||
this.log(interfaces_1.LogLevel.INFO, component, message, metadata); | ||
} | ||
warn(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.WARN, component, message, metadata); | ||
this.log(interfaces_1.LogLevel.WARN, component, message, metadata); | ||
} | ||
error(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.ERROR, component, message, metadata); | ||
this.log(interfaces_1.LogLevel.ERROR, component, message, metadata); | ||
} | ||
@@ -193,3 +131,3 @@ deprecate(component, alternative, methodOverride) { | ||
args.push(e.stack); | ||
this.log(LogLevel_1.LogLevel.WARN, component, args.join('\n')); | ||
this.log(interfaces_1.LogLevel.WARN, component, args.join('\n')); | ||
} | ||
@@ -205,3 +143,3 @@ deprecateParameterType(component, argumentLocation, deprecatedType, alternative) { | ||
args.push(e.stack); | ||
this.log(LogLevel_1.LogLevel.WARN, component, args.join('\n')); | ||
this.log(interfaces_1.LogLevel.WARN, component, args.join('\n')); | ||
} | ||
@@ -208,0 +146,0 @@ $getDeprecatedMethodMessage(e) { |
{ | ||
"name": "@arashi/logger", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Logger", | ||
@@ -11,15 +11,21 @@ "main": "./lib/api.js", | ||
"dependencies": { | ||
"events": "3.3.0", | ||
"util": "0.12.4", | ||
"winston": "3.3.3" | ||
}, | ||
"peerDependencies": { | ||
"@arashi/interfaces": ">=1.1.0 <2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@totalpave/eslint-plugin": "6.0.0", | ||
"@types/jest": "27.0.2", | ||
"@types/node": "16.11.6", | ||
"@typescript-eslint/eslint-plugin": "5.2.0", | ||
"@typescript-eslint/parser": "5.2.0", | ||
"@arashi/interfaces": "1.1.0", | ||
"@totalpave/eslint-plugin": "6.0.1", | ||
"@types/jest": "27.0.3", | ||
"@types/node": "16.11.11", | ||
"@typescript-eslint/eslint-plugin": "5.5.0", | ||
"@typescript-eslint/parser": "5.5.0", | ||
"auto-changelog": "2.3.0", | ||
"eslint": "8.1.0", | ||
"jest": "27.3.1", | ||
"eslint": "8.3.0", | ||
"jest": "27.4.2", | ||
"nyc": "15.1.0", | ||
"ts-jest": "27.0.5", | ||
"ts-jest": "27.0.7", | ||
"ts-node": "10.4.0", | ||
@@ -26,0 +32,0 @@ "typedoc": "0.21.5", |
export {ILogEvent} from './ILogEvent'; | ||
export {LogEvent} from './LogEvent'; | ||
export {Logger} from './Logger'; | ||
export {LogLevel} from './LogLevel'; | ||
export {Logger, ILogMetadata} from './Logger'; | ||
export {ConsoleLogger} from './ConsoleLogger'; |
@@ -17,125 +17,134 @@ // Copyright (C) 2020 Norman Breau | ||
import * as utils from 'util'; | ||
import * as Winston from 'winston'; | ||
import { LogLevel } from './LogLevel'; | ||
// import * as Winston from 'winston'; | ||
import { | ||
LogLevel, | ||
LogEvent, | ||
ILogger | ||
} from '@arashi/interfaces'; | ||
import {EventEmitter} from 'events'; | ||
import {LogEvent} from './LogEvent'; | ||
import * as Path from 'path'; | ||
// import * as Path from 'path'; | ||
const F_RESET: string = '\x1b[0m'; | ||
const F_DIM: string = '\x1b[2m'; | ||
const F_FG_BLUE: string = '\x1b[34m'; | ||
const F_FG_CYAN: string = '\x1b[36m'; | ||
// const F_RESET: string = '\x1b[0m'; | ||
// const F_DIM: string = '\x1b[2m'; | ||
// const F_FG_BLUE: string = '\x1b[34m'; | ||
// const F_FG_CYAN: string = '\x1b[36m'; | ||
const DEFAULT_MAX_FILE_SIZE: number = 52428800; // 50MB | ||
// const DEFAULT_MAX_FILE_SIZE: number = 52428800; // 50MB | ||
export class Logger extends EventEmitter { | ||
export interface ILogMetadata { | ||
service: string; | ||
component: string; | ||
meta: Record<any, any>; | ||
} | ||
export abstract class Logger extends EventEmitter implements ILogger { | ||
private $filters: Array<RegExp>; | ||
private $logger: Winston.Logger; | ||
private $logLocation: string; | ||
// private $logger: Winston.Logger; | ||
// private $logLocation: string; | ||
private $logLevel: LogLevel; | ||
private $serviceName: string; | ||
public constructor(serviceName: string = 'Generic', logLevel: LogLevel = LogLevel.INFO, logLocation?: string) { | ||
public constructor(serviceName: string = 'Generic', logLevel: LogLevel = LogLevel.INFO) { | ||
super(); | ||
this.$logLocation = logLocation ? Path.resolve(logLocation) : null; | ||
this.$filters = this._getDefaultLogFilters(); | ||
this.$serviceName = serviceName; | ||
this.$logLevel = logLevel; | ||
let format: Winston.Logform.Format = Winston.format((info: Winston.Logform.TransformableInfo, opts?: any): Winston.Logform.TransformableInfo => { | ||
// Typescript for some reason doesn't allow using symbols as indexes. | ||
const MESSAGE: any = Symbol.for('message'); | ||
info[MESSAGE] = `${F_DIM}${info.timestamp}${F_RESET} - [${F_FG_BLUE}${info.service}${F_RESET}][${F_FG_CYAN}${info.component}${F_RESET}]: ${info.level}: ${info.message}` | ||
return info; | ||
})(); | ||
// let format: Winston.Logform.Format = Winston.format((info: Winston.Logform.TransformableInfo, opts?: any): Winston.Logform.TransformableInfo => { | ||
// // Typescript for some reason doesn't allow using symbols as indexes. | ||
// const MESSAGE: any = Symbol.for('message'); | ||
// info[MESSAGE] = `${F_DIM}${info.timestamp}${F_RESET} - [${F_FG_BLUE}${info.service}${F_RESET}][${F_FG_CYAN}${info.component}${F_RESET}]: ${info.level}: ${info.message}` | ||
// return info; | ||
// })(); | ||
let consoleTransport: Winston.transports.ConsoleTransportInstance = new Winston.transports.Console({ | ||
format: Winston.format.combine( | ||
Winston.format.colorize(), | ||
format, | ||
Winston.format.errors({ stack: true }) | ||
) | ||
}); | ||
// let consoleTransport: Winston.transports.ConsoleTransportInstance = new Winston.transports.Console({ | ||
// format: Winston.format.combine( | ||
// Winston.format.colorize(), | ||
// format, | ||
// Winston.format.errors({ stack: true }) | ||
// ) | ||
// }); | ||
consoleTransport.on('logged', (log: Winston.LogEntry) => { | ||
this.emit(LogEvent.LOG, log.message); | ||
}); | ||
// consoleTransport.on('logged', (log: Winston.LogEntry) => { | ||
// this.emit(LogEvent.LOG, log.message); | ||
// }); | ||
let transports: Array<Winston.transport> = [ consoleTransport ]; | ||
// let transports: Array<Winston.transport> = [ consoleTransport ]; | ||
if (this.$logLocation) { | ||
transports.push(new Winston.transports.File({ | ||
filename: Path.resolve(this.$logLocation, `${serviceName}.json.log`), | ||
level: logLevel, | ||
maxsize: this.getMaxFileSize(), | ||
format: Winston.format.combine( | ||
Winston.format.json(), | ||
Winston.format.errors({ stack: true }), | ||
Winston.format((info: Winston.Logform.TransformableInfo, opts?: any): Winston.Logform.TransformableInfo => { | ||
const MESSAGE: any = Symbol.for('message'); | ||
info[MESSAGE] = utils.inspect({ | ||
level: info.level, | ||
message: info.message, | ||
timestamp: info.timestamp, | ||
service: info.service, | ||
component: info.component, | ||
meta: info.meta | ||
}, { | ||
depth: Infinity, | ||
colors: false, | ||
maxArrayLength: Infinity, | ||
showProxy: true, | ||
breakLength: Infinity | ||
}); | ||
return info; | ||
})() | ||
) | ||
})); | ||
transports.push(new Winston.transports.File({ | ||
filename: Path.resolve(this.$logLocation, `${serviceName}.log`), | ||
maxsize: this.getMaxFileSize(), | ||
level: logLevel, | ||
format: Winston.format.combine( | ||
Winston.format.simple(), | ||
Winston.format.errors({ stack: true }), | ||
format | ||
) | ||
})); | ||
transports.push(new Winston.transports.File({ | ||
filename: Path.resolve(this.$logLocation, `${serviceName}.errors.log`), | ||
maxsize: this.getMaxFileSize(), | ||
level: LogLevel.WARN, | ||
format: Winston.format.combine( | ||
Winston.format.simple(), | ||
Winston.format.errors({ stack: true }), | ||
format | ||
) | ||
})); | ||
} | ||
// if (this.$logLocation) { | ||
// transports.push(new Winston.transports.File({ | ||
// filename: Path.resolve(this.$logLocation, `${serviceName}.json.log`), | ||
// level: logLevel, | ||
// maxsize: this.getMaxFileSize(), | ||
// format: Winston.format.combine( | ||
// Winston.format.json(), | ||
// Winston.format.errors({ stack: true }), | ||
// Winston.format((info: Winston.Logform.TransformableInfo, opts?: any): Winston.Logform.TransformableInfo => { | ||
// const MESSAGE: any = Symbol.for('message'); | ||
// info[MESSAGE] = utils.inspect({ | ||
// level: info.level, | ||
// message: info.message, | ||
// timestamp: info.timestamp, | ||
// service: info.service, | ||
// component: info.component, | ||
// meta: info.meta | ||
// }, { | ||
// depth: Infinity, | ||
// colors: false, | ||
// maxArrayLength: Infinity, | ||
// showProxy: true, | ||
// breakLength: Infinity | ||
// }); | ||
// return info; | ||
// })() | ||
// ) | ||
// })); | ||
// transports.push(new Winston.transports.File({ | ||
// filename: Path.resolve(this.$logLocation, `${serviceName}.log`), | ||
// maxsize: this.getMaxFileSize(), | ||
// level: logLevel, | ||
// format: Winston.format.combine( | ||
// Winston.format.simple(), | ||
// Winston.format.errors({ stack: true }), | ||
// format | ||
// ) | ||
// })); | ||
// transports.push(new Winston.transports.File({ | ||
// filename: Path.resolve(this.$logLocation, `${serviceName}.errors.log`), | ||
// maxsize: this.getMaxFileSize(), | ||
// level: LogLevel.WARN, | ||
// format: Winston.format.combine( | ||
// Winston.format.simple(), | ||
// Winston.format.errors({ stack: true }), | ||
// format | ||
// ) | ||
// })); | ||
// } | ||
this.$logger = Winston.createLogger({ | ||
level: logLevel, | ||
format: Winston.format.combine( | ||
Winston.format.timestamp({ | ||
format: 'YYYY-MM-DD HH:mm:ss' | ||
}), | ||
Winston.format.errors({ stack: true }) | ||
), | ||
defaultMeta: { | ||
service: serviceName | ||
}, | ||
transports: transports | ||
}); | ||
// this.$logger = Winston.createLogger({ | ||
// level: logLevel, | ||
// format: Winston.format.combine( | ||
// Winston.format.timestamp({ | ||
// format: 'YYYY-MM-DD HH:mm:ss' | ||
// }), | ||
// Winston.format.errors({ stack: true }) | ||
// ), | ||
// defaultMeta: { | ||
// service: serviceName | ||
// }, | ||
// transports: transports | ||
// }); | ||
} | ||
public getMaxFileSize(): number { | ||
return DEFAULT_MAX_FILE_SIZE; | ||
} | ||
// public getMaxFileSize(): number { | ||
// return DEFAULT_MAX_FILE_SIZE; | ||
// } | ||
public setLogLevel(level: LogLevel): void { | ||
this.$logger.level = level; | ||
this.$logLevel = level; | ||
} | ||
public getLogLevel(): LogLevel { | ||
return <LogLevel> this.$logger.level; | ||
return this.$logLevel; | ||
} | ||
@@ -207,2 +216,4 @@ | ||
protected abstract _log(logLevel: LogLevel, message: string, metadata: ILogMetadata): void; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
@@ -216,3 +227,4 @@ public log(level: LogLevel, component: string, message: any, metadata?: Record<any, any>): void { | ||
if (this._shouldFilter(message)) { | ||
this.$logger.log(level, this.$parseMessage(message), { | ||
let msg: string = this.$parseMessage(message); | ||
this._log(level, msg, { | ||
service: this.$serviceName, | ||
@@ -222,2 +234,10 @@ component: component, | ||
}); | ||
this.emit(LogEvent.LOG, { | ||
level: level, | ||
message: msg, | ||
service: this.$serviceName, | ||
component: component, | ||
meta: metadata | ||
}); | ||
} | ||
@@ -224,0 +244,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
115891
4
15
34
586
+ Addedevents@3.3.0
+ Addedutil@0.12.4
+ Added@arashi/interfaces@1.2.1(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedis-arguments@1.2.0(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.1.0(transitive)
+ Addedis-regex@1.2.1(transitive)
+ Addedis-typed-array@1.1.15(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedsafe-regex-test@1.1.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedutil@0.12.4(transitive)
+ Addedwhich-typed-array@1.1.18(transitive)