@matrixai/logger
Advanced tools
Comparing version 1.0.4 to 2.0.0
export { default } from './Logger'; | ||
export { default as Handler } from './Handler'; | ||
export * from './handlers'; | ||
export * as formatting from './formatting'; | ||
export * from './types'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,4 +28,9 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = void 0; | ||
exports.formatting = exports.Handler = exports.default = void 0; | ||
var Logger_1 = require("./Logger"); | ||
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(Logger_1).default; } }); | ||
var Handler_1 = require("./Handler"); | ||
Object.defineProperty(exports, "Handler", { enumerable: true, get: function () { return __importDefault(Handler_1).default; } }); | ||
__exportStar(require("./handlers"), exports); | ||
exports.formatting = __importStar(require("./formatting")); | ||
__exportStar(require("./types"), exports); |
@@ -0,26 +1,30 @@ | ||
import type { ToString, LogRecord } from './types'; | ||
import { LogLevel } from './types'; | ||
import Handler from './Handler'; | ||
declare class Logger { | ||
name: string; | ||
private parent; | ||
private loggers; | ||
private handlers; | ||
private level; | ||
constructor(name?: string, parent?: any, level?: number); | ||
getLogger(name: string): Logger; | ||
info(msg: string): void; | ||
warn(msg: string): void; | ||
debug(msg: string): void; | ||
error(msg: string): void; | ||
getLevelName(): string; | ||
setLevel(level: number): void; | ||
isEnabledFor(level: number): boolean; | ||
getName(): string; | ||
getChild(suffix: string): Logger; | ||
getParent(): Logger; | ||
addHandler(handler: any): void; | ||
removeHandler(handler: any): void; | ||
key: string; | ||
level: LogLevel; | ||
readonly handlers: Set<Handler>; | ||
readonly parent?: Logger; | ||
readonly loggers: { | ||
[key: string]: Logger; | ||
}; | ||
constructor(key?: string, level?: LogLevel, handlers?: Array<Handler>, parent?: Logger); | ||
getChild(key: string): Logger; | ||
getParent(): Logger | undefined; | ||
setLevel(level: LogLevel): void; | ||
getEffectiveLevel(): LogLevel; | ||
isEnabledFor(level: LogLevel): boolean; | ||
addHandler(handler: Handler): void; | ||
removeHandler(handler: Handler): void; | ||
clearHandlers(): void; | ||
hasHandlers(): boolean; | ||
private makeRecord; | ||
private log; | ||
private callHandlers; | ||
debug(data: ToString): void; | ||
info(data: ToString): void; | ||
warn(data: ToString): void; | ||
error(data: ToString): void; | ||
protected log(msg: string, level: LogLevel): void; | ||
protected makeRecord(msg: string, level: LogLevel): LogRecord; | ||
protected callHandlers(record: LogRecord): void; | ||
} | ||
export default Logger; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const types_1 = require("./types"); | ||
const ConsoleHandler_1 = __importDefault(require("./handlers/ConsoleHandler")); | ||
class Logger { | ||
constructor(name, parent, level) { | ||
this.name = name !== null && name !== void 0 ? name : 'root'; | ||
this.parent = parent !== null && parent !== void 0 ? parent : null; | ||
this.handlers = []; | ||
this.addHandler(new ConsoleHandler()); | ||
this.level = level !== null && level !== void 0 ? level : 0; | ||
this.loggers = []; | ||
constructor(key = 'root', level = types_1.LogLevel.NOTSET, handlers = [new ConsoleHandler_1.default()], parent) { | ||
this.loggers = {}; | ||
this.key = key; | ||
this.level = level; | ||
this.handlers = new Set(handlers); | ||
this.parent = parent; | ||
} | ||
getLogger(name) { | ||
if (this.loggers[name]) { | ||
return this.loggers[name]; | ||
getChild(key) { | ||
if (this.loggers[key]) { | ||
return this.loggers[key]; | ||
} | ||
let checkParent = this.parent; | ||
while (checkParent) { | ||
if (checkParent.loggers[name]) { | ||
return checkParent.loggers[name]; | ||
} | ||
checkParent = this.parent; | ||
} | ||
this.loggers[name] = new Logger(name, this); | ||
return this.loggers[name]; | ||
const logger = new Logger(key, types_1.LogLevel.NOTSET, [], this); | ||
this.loggers[key] = logger; | ||
return logger; | ||
} | ||
info(msg) { | ||
this.log(msg, 2); | ||
getParent() { | ||
return this.parent; | ||
} | ||
warn(msg) { | ||
this.log(msg, 3); | ||
setLevel(level) { | ||
this.level = level; | ||
} | ||
debug(msg) { | ||
this.log(msg, 1); | ||
} | ||
error(msg) { | ||
this.log(msg, 4); | ||
} | ||
getLevelName() { | ||
let levelName = ''; | ||
if (this.level === 1) { | ||
levelName = 'DEBUG'; | ||
getEffectiveLevel() { | ||
if (this.level !== types_1.LogLevel.NOTSET) { | ||
return this.level; | ||
} | ||
else if (this.level === 2) { | ||
levelName = 'INFO'; | ||
if (this.parent) { | ||
return this.parent.getEffectiveLevel(); | ||
} | ||
else if (this.level === 3) { | ||
levelName = 'WARN'; | ||
} | ||
else if (this.level === 4) { | ||
levelName = 'INFO'; | ||
} | ||
else { | ||
levelName = 'NOTSET'; | ||
} | ||
return levelName; | ||
return this.level; | ||
} | ||
setLevel(level) { | ||
this.level = level; | ||
} | ||
isEnabledFor(level) { | ||
return level >= this.level; | ||
} | ||
getName() { | ||
return this.name; | ||
} | ||
getChild(suffix) { | ||
if (!suffix) { | ||
throw new Error('Argument 1 of Logger.getChild is not specified.'); | ||
} | ||
if (!this.loggers[suffix]) { | ||
throw new Error(`Logger has no child with name ${suffix}`); | ||
} | ||
return this.loggers[suffix]; | ||
} | ||
getParent() { | ||
return this.parent; | ||
} | ||
addHandler(handler) { | ||
if (typeof handler !== 'object') { | ||
throw new Error('Argument 1 of Logger.addHandler is not an object.'); | ||
} | ||
this.handlers.push(handler); | ||
this.handlers.add(handler); | ||
} | ||
removeHandler(handler) { | ||
const index = this.handlers.indexOf(handler); | ||
if (index > -1) { | ||
this.handlers.splice(index, 1); | ||
} | ||
this.handlers.delete(handler); | ||
} | ||
clearHandlers() { | ||
this.handlers.clear(); | ||
} | ||
hasHandlers() { | ||
if (this.handlers.length) { | ||
var _a, _b; | ||
if (this.handlers.size) { | ||
return true; | ||
} | ||
return false; | ||
else { | ||
return (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.hasHandlers()) !== null && _b !== void 0 ? _b : false; | ||
} | ||
} | ||
makeRecord(msg, level) { | ||
const date = new Date(); | ||
const record = { | ||
created: `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()} @ ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`, | ||
level: level, | ||
name: this.name, | ||
message: msg, | ||
}; | ||
return record; | ||
debug(data) { | ||
this.log(data.toString(), types_1.LogLevel.DEBUG); | ||
} | ||
info(data) { | ||
this.log(data.toString(), types_1.LogLevel.INFO); | ||
} | ||
warn(data) { | ||
this.log(data.toString(), types_1.LogLevel.WARN); | ||
} | ||
error(data) { | ||
this.log(data.toString(), types_1.LogLevel.ERROR); | ||
} | ||
log(msg, level) { | ||
const record = this.makeRecord(msg, level); | ||
if (level >= this.level) { | ||
if (level >= this.getEffectiveLevel()) { | ||
this.callHandlers(record); | ||
} | ||
} | ||
makeRecord(msg, level) { | ||
return { | ||
key: this.key, | ||
date: new Date(), | ||
msg: msg, | ||
level: level, | ||
}; | ||
} | ||
callHandlers(record) { | ||
if (this.hasHandlers()) { | ||
this.handlers.forEach((handler) => { | ||
handler.handle(record); | ||
}); | ||
for (const handler of this.handlers) { | ||
handler.handle(record); | ||
} | ||
else { | ||
if (this.parent) { | ||
this.parent.callHandlers(record); | ||
@@ -123,15 +95,2 @@ } | ||
} | ||
class Handler { | ||
handle(record) { | ||
this.emit(record); | ||
} | ||
} | ||
class ConsoleHandler extends Handler { | ||
constructor() { | ||
super(); | ||
} | ||
emit(record) { | ||
console.error(record.created, ': ', record.name, ': ', record.message); | ||
} | ||
} | ||
exports.default = Logger; |
{ | ||
"name": "@matrixai/logger", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"author": "Scott Morris", | ||
@@ -5,0 +5,0 @@ "description": "Python-like JavaScript Logger", |
@@ -13,52 +13,2 @@ # js-logger | ||
## Usage | ||
### Hierarchical Loggers | ||
Generate sub-loggers for different domains of a project | ||
```sh | ||
import Logger from '@matrixai/js-logger' | ||
const logger = new Logger(); | ||
const subLogger = logger.getLogger('scope'); | ||
``` | ||
Parents and childs of loggers can be gotten | ||
```sh | ||
const subLogger = logger.getChild('scope'); | ||
const newLogger = subLogger.getParent(); | ||
``` | ||
### Message types | ||
Can log 4 different types of messages | ||
```sh | ||
logger.debug('output'); | ||
logger.info('output'); | ||
logger.warn('output'); | ||
logger.error('output'); | ||
``` | ||
Can set the level of loggers to control the verbosity | ||
```sh | ||
logger.setLevel(3); | ||
# These will not be logged | ||
logger.debug('output'); | ||
logger.info('output'); | ||
# These will be logged | ||
logger.warn('output'); | ||
logger.error('output'); | ||
``` | ||
## Development | ||
@@ -65,0 +15,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26512
21
381
42
1