@4lch4/logger
Advanced tools
Comparing version 1.4.1 to 1.6.0
import { ILogger, ILoggerOpts } from './interfaces'; | ||
export declare class Logger implements ILogger { | ||
private formatter; | ||
private ioUtil?; | ||
constructor(loggerOpts?: ILoggerOpts); | ||
info(msg: string): void; | ||
warn(msg: string): void; | ||
debug(msg: string): void; | ||
info(msg: string | Object): void; | ||
warn(msg: string | Object): void; | ||
debug(msg: string | Object): void; | ||
error(msg: string | Error, ...extra: any[]): void; | ||
@@ -10,0 +9,0 @@ success(msg: string): void; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const lib_1 = require("./lib"); | ||
const IOUtil_1 = require("./lib/IOUtil"); | ||
class Logger { | ||
@@ -16,5 +15,2 @@ constructor(loggerOpts) { | ||
colorOpts = loggerOpts.colorOpts; | ||
if (loggerOpts === null || loggerOpts === void 0 ? void 0 : loggerOpts.logDir) { | ||
this.ioUtil = new IOUtil_1.IOUtil(loggerOpts.logDir, loggerOpts.format); | ||
} | ||
this.formatter = new lib_1.Formatter(formatOpt, colorOpts); | ||
@@ -24,14 +20,8 @@ } | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.info)); | ||
if (this.ioUtil) | ||
this.ioUtil.writeToLog(msg, interfaces_1.Level.info); | ||
} | ||
warn(msg) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.warn)); | ||
if (this.ioUtil) | ||
this.ioUtil.writeToLog(msg, interfaces_1.Level.warn); | ||
} | ||
debug(msg) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.debug)); | ||
if (this.ioUtil) | ||
this.ioUtil.writeToLog(msg, interfaces_1.Level.debug); | ||
} | ||
@@ -48,21 +38,8 @@ error(msg, ...extra) { | ||
} | ||
if (this.ioUtil) { | ||
if (msg instanceof Error) | ||
this.ioUtil.writeToLog(msg.message, interfaces_1.Level.error); | ||
else | ||
this.ioUtil.writeToLog(msg, interfaces_1.Level.error); | ||
if (extra.length > 0) { | ||
this.ioUtil.writeToLog(extra.join('\n'), interfaces_1.Level.error); | ||
} | ||
} | ||
} | ||
success(msg) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.success)); | ||
if (this.ioUtil) | ||
this.ioUtil.writeToLog(msg, interfaces_1.Level.success); | ||
} | ||
log(msg, level) { | ||
console.log(this.formatter.formatMsg(msg, level)); | ||
if (this.ioUtil) | ||
this.ioUtil.writeToLog(msg, level); | ||
} | ||
@@ -69,0 +46,0 @@ } |
@@ -5,3 +5,2 @@ import { IColorOpts, LogFormat } from '.'; | ||
colorOpts?: IColorOpts; | ||
logDir?: string; | ||
} |
export * from './IColorOpts'; | ||
export * from './ILogFilePaths'; | ||
export * from './ILogger'; | ||
@@ -4,0 +3,0 @@ export * from './ILoggerOpts'; |
@@ -14,3 +14,2 @@ "use strict"; | ||
__exportStar(require("./IColorOpts"), exports); | ||
__exportStar(require("./ILogFilePaths"), exports); | ||
__exportStar(require("./ILogger"), exports); | ||
@@ -17,0 +16,0 @@ __exportStar(require("./ILoggerOpts"), exports); |
@@ -8,5 +8,5 @@ import { IColorOpts, Level } from '../interfaces'; | ||
colorMsg(msg: string, level: Level): string; | ||
formatMsg(msg: string, level: Level): string; | ||
formatMsg(msg: string | Object, level: Level): string; | ||
getUTCTime(): string; | ||
getPrettyTime(): string; | ||
} |
@@ -22,20 +22,26 @@ "use strict"; | ||
formatMsg(msg, level) { | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
time: this.getUTCTime(), | ||
host: os_1.hostname(), | ||
pid: process.pid, | ||
level, | ||
msg | ||
}; | ||
return this.colorMsg(JSON.stringify(logOutput), level); | ||
switch (typeof msg) { | ||
case 'string': { | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
time: this.getUTCTime(), | ||
host: os_1.hostname(), | ||
pid: process.pid, | ||
level, | ||
msg | ||
}; | ||
return this.colorMsg(JSON.stringify(logOutput), level); | ||
} | ||
else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
msg | ||
]; | ||
return this.colorMsg(output.join(' - '), level); | ||
} | ||
} | ||
case 'object': | ||
return this.colorMsg(JSON.stringify(msg), level); | ||
} | ||
else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
msg | ||
]; | ||
return this.colorMsg(output.join(' - '), level); | ||
} | ||
} | ||
@@ -42,0 +48,0 @@ getUTCTime() { |
export * from './defaults'; | ||
export * from './Formatter'; | ||
export * from './legacy'; |
@@ -15,3 +15,2 @@ "use strict"; | ||
__exportStar(require("./Formatter"), exports); | ||
__exportStar(require("./legacy"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@4lch4/logger", | ||
"displayName": "Logger", | ||
"version": "1.4.1", | ||
"version": "1.6.0", | ||
"description": "A small utility for logging to console within NodeJS/TypeScript applications.", | ||
@@ -38,10 +38,14 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"chalk": "^4.1.0", | ||
"dayjs": "^1.10.4", | ||
"fs-extra": "^9.1.0" | ||
"chalk": "^4.1.2", | ||
"dayjs": "^1.10.7", | ||
"fs-extra": "^10.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "^9.0.8", | ||
"np": "^7.4.0" | ||
"@types/fs-extra": "^9.0.12", | ||
"@types/node": "^16.9.1", | ||
"np": "^7.5.0", | ||
"prettier": "^2.4.0", | ||
"ts-node": "^10.2.1", | ||
"typescript": "^4.4.3" | ||
} | ||
} |
import { ILogger, ILoggerOpts, Level } from './interfaces' | ||
import { DefaultColors, DefaultLogFormat, Formatter } from './lib' | ||
import { IOUtil } from './lib/IOUtil' | ||
export class Logger implements ILogger { | ||
private formatter: Formatter | ||
private ioUtil?: IOUtil | ||
@@ -15,5 +13,2 @@ constructor(loggerOpts?: ILoggerOpts) { | ||
if (loggerOpts?.colorOpts) colorOpts = loggerOpts.colorOpts | ||
if (loggerOpts?.logDir) { | ||
this.ioUtil = new IOUtil(loggerOpts.logDir, loggerOpts.format) | ||
} | ||
@@ -23,25 +18,21 @@ this.formatter = new Formatter(formatOpt, colorOpts) | ||
info(msg: string) { | ||
info(msg: string | Object) { | ||
console.log(this.formatter.formatMsg(msg, Level.info)) | ||
if (this.ioUtil) this.ioUtil.writeToLog(msg, Level.info) | ||
} | ||
warn(msg: string) { | ||
warn(msg: string | Object) { | ||
console.log(this.formatter.formatMsg(msg, Level.warn)) | ||
if (this.ioUtil) this.ioUtil.writeToLog(msg, Level.warn) | ||
} | ||
debug(msg: string) { | ||
debug(msg: string | Object) { | ||
console.log(this.formatter.formatMsg(msg, Level.debug)) | ||
if (this.ioUtil) this.ioUtil.writeToLog(msg, Level.debug) | ||
} | ||
error(msg: string | Error, ...extra: any[]) { | ||
error(msg: string | Error | unknown, ...extra: any[]) { | ||
if (msg instanceof Error) { | ||
console.log(this.formatter.formatMsg(msg.message, Level.error)) | ||
} else if (msg instanceof String) { | ||
console.log(this.formatter.formatMsg(msg, Level.error)) | ||
} else { | ||
console.log(this.formatter.formatMsg(msg, Level.error)) | ||
console.log(this.formatter.formatMsg(msg as Object, Level.error)) | ||
} | ||
@@ -52,11 +43,2 @@ | ||
} | ||
if (this.ioUtil) { | ||
if (msg instanceof Error) this.ioUtil.writeToLog(msg.message, Level.error) | ||
else this.ioUtil.writeToLog(msg, Level.error) | ||
if (extra.length > 0) { | ||
this.ioUtil.writeToLog(extra.join('\n'), Level.error) | ||
} | ||
} | ||
} | ||
@@ -66,4 +48,2 @@ | ||
console.log(this.formatter.formatMsg(msg, Level.success)) | ||
if (this.ioUtil) this.ioUtil.writeToLog(msg, Level.success) | ||
} | ||
@@ -73,4 +53,2 @@ | ||
console.log(this.formatter.formatMsg(msg, level)) | ||
if (this.ioUtil) this.ioUtil.writeToLog(msg, level) | ||
} | ||
@@ -77,0 +55,0 @@ } |
@@ -9,8 +9,2 @@ import { IColorOpts, LogFormat } from '.' | ||
colorOpts?: IColorOpts | ||
/** | ||
* An optional field that specifies where to store log files. If this property | ||
* is left empty, then no files are created and logging only goes to stdout. | ||
*/ | ||
logDir?: string | ||
} |
export * from './IColorOpts' | ||
export * from './ILogFilePaths' | ||
export * from './ILogger' | ||
@@ -7,1 +6,2 @@ export * from './ILoggerOpts' | ||
export * from './LogFormat' | ||
@@ -24,21 +24,27 @@ import chalk from 'chalk' | ||
formatMsg(msg: string, level: Level) { | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
/** The timestamp for when the message was sent. */ | ||
time: this.getUTCTime(), | ||
host: hostname(), | ||
pid: process.pid, | ||
level, | ||
msg | ||
formatMsg(msg: string | Object, level: Level) { | ||
switch (typeof msg) { | ||
case 'string': { | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
/** The timestamp for when the message was sent. */ | ||
time: this.getUTCTime(), | ||
host: hostname(), | ||
pid: process.pid, | ||
level, | ||
msg | ||
} | ||
return this.colorMsg(JSON.stringify(logOutput), level) | ||
} else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
msg | ||
] | ||
return this.colorMsg(output.join(' - '), level) | ||
} | ||
} | ||
return this.colorMsg(JSON.stringify(logOutput), level) | ||
} else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
msg | ||
] | ||
return this.colorMsg(output.join(' - '), level) | ||
case 'object': | ||
return this.colorMsg(JSON.stringify(msg), level) | ||
} | ||
@@ -45,0 +51,0 @@ } |
export * from './defaults' | ||
export * from './Formatter' | ||
export * from './legacy' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
34318
6
51
413
+ Addedfs-extra@10.1.0(transitive)
- Removedat-least-node@1.0.0(transitive)
- Removedfs-extra@9.1.0(transitive)
Updatedchalk@^4.1.2
Updateddayjs@^1.10.7
Updatedfs-extra@^10.0.0