@ascot/log
Advanced tools
Comparing version 2.0.1 to 2.0.10
@@ -1,34 +0,3 @@ | ||
export declare type LogLevel = "error" | "warn" | "info" | "verbose" | "debug" | "silly" | false; | ||
export declare const LogLevelPriorities: { | ||
[level: string]: number; | ||
}; | ||
export declare const LogLevelColors: { | ||
[level: string]: Function; | ||
}; | ||
export declare const LogLevelKeys: { | ||
[level: string]: string; | ||
}; | ||
export interface LoggerConfig { | ||
name: string; | ||
level: LogLevel; | ||
pid?: boolean; | ||
} | ||
export declare type LoggerMessage = string | number | object | Error; | ||
export interface LoggerMessageOptions { | ||
args?: any[]; | ||
prefix?: string; | ||
timestampMs?: boolean; | ||
} | ||
declare module logs { | ||
function setup(config: LoggerConfig): void; | ||
function silly(message: LoggerMessage, options?: LoggerMessageOptions): void; | ||
function verbose(message: LoggerMessage, options?: LoggerMessageOptions): void; | ||
function info(message: LoggerMessage, options?: LoggerMessageOptions): void; | ||
function warn(message: LoggerMessage, options?: LoggerMessageOptions): void; | ||
function error(message: LoggerMessage, options?: LoggerMessageOptions): void; | ||
function debug(message: LoggerMessage, options?: LoggerMessageOptions): void; | ||
function log(level: LogLevel, message: LoggerMessage, options?: LoggerMessageOptions): void; | ||
function className(type: new () => any): string; | ||
const colors: import("chalk").ChalkInstance; | ||
} | ||
import { logs } from "./logger"; | ||
export * from "./models"; | ||
export default logs; |
172
index.js
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
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); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LogLevelKeys = exports.LogLevelColors = exports.LogLevelPriorities = void 0; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const path_1 = __importDefault(require("path")); | ||
const DEFAULT_LOG_LEVEL = "verbose"; | ||
exports.LogLevelPriorities = { | ||
error: 0, | ||
warn: 1, | ||
info: 2, | ||
verbose: 3, | ||
debug: 4, | ||
silly: 5, | ||
}; | ||
exports.LogLevelColors = { | ||
error: chalk_1.default.red, | ||
warn: chalk_1.default.yellow, | ||
info: chalk_1.default.cyan, | ||
verbose: chalk_1.default.magenta, | ||
debug: chalk_1.default.blue, | ||
silly: chalk_1.default.gray, | ||
}; | ||
exports.LogLevelKeys = { | ||
error: "ERROR", | ||
warn: "WARN", | ||
info: "INFO", | ||
verbose: "VERB", | ||
debug: "DEBUG", | ||
silly: "SILLY", | ||
}; | ||
var logs; | ||
(function (logs) { | ||
let loggerConfig = { | ||
name: null, | ||
level: DEFAULT_LOG_LEVEL | ||
}; | ||
function setup(config) { | ||
loggerConfig = config; | ||
process.on("unhandledRejection", (reason, promise) => onUnhandledRejection(reason, promise)); | ||
process.on("uncaughtException", (err) => onUncaughtException(err)); | ||
} | ||
logs.setup = setup; | ||
function silly(message, options) { | ||
return log("silly", message, options || {}); | ||
} | ||
logs.silly = silly; | ||
function verbose(message, options) { | ||
return log("verbose", message, options || {}); | ||
} | ||
logs.verbose = verbose; | ||
function info(message, options) { | ||
return log("info", message, options || {}); | ||
} | ||
logs.info = info; | ||
function warn(message, options) { | ||
return log("warn", message, options || {}); | ||
} | ||
logs.warn = warn; | ||
function error(message, options) { | ||
return log("error", message, options || {}); | ||
} | ||
logs.error = error; | ||
function debug(message, options) { | ||
return log("debug", message, options || {}); | ||
} | ||
logs.debug = debug; | ||
function log(level, message, options) { | ||
options = options || {}; | ||
if (loggerConfig && !loggerConfig.name) { | ||
try { | ||
loggerConfig.name = require(path_1.default.join(process.cwd(), "package.json")).name; | ||
} | ||
catch (err) { | ||
} | ||
} | ||
const maxLevel = loggerConfig.level; | ||
const shouldLog = maxLevel != false && exports.LogLevelPriorities[maxLevel] >= exports.LogLevelPriorities[level]; | ||
if (shouldLog) | ||
logSTD(level, message, options); | ||
// | ||
// More logging handlers (e.g. papertrail, newrelic, ...) can be added here | ||
// | ||
} | ||
logs.log = log; | ||
function padLeft(nr, n, str) { | ||
return Array(n - String(nr).length + 1).join(str || "0") + nr; | ||
} | ||
function timestamp(includeMilli = false) { | ||
const now = new Date(); | ||
let timestamp = padLeft(now.getHours(), 2) + ":" + padLeft(now.getMinutes(), 2) + ":" + padLeft(now.getSeconds(), 2); | ||
if (includeMilli) | ||
timestamp += "." + padLeft(now.getMilliseconds(), 3); | ||
return timestamp; | ||
} | ||
function logSTD(level, message, options) { | ||
if (level == false) | ||
return; | ||
function column(value, maxLength) { | ||
value = value.slice(0, Math.min(maxLength, value.length)); | ||
return value.padEnd(maxLength, ' '); | ||
} | ||
const typeFn = exports.LogLevelColors[level]; | ||
let prefix = options && options.prefix; | ||
let name = loggerConfig.name || "?"; | ||
let ms = options.timestampMs != null ? options.timestampMs : true; | ||
let out = ""; | ||
if (loggerConfig.pid == true) | ||
out += chalk_1.default.gray(`(${process.pid}) `); | ||
out += logs.colors.green(column(timestamp(ms), ms ? 17 : 13)) + typeFn(column(exports.LogLevelKeys[level], 7)) + chalk_1.default.yellow(column(name, 12)) + " "; | ||
// if (prefix != null && prefix.trim() !== "") out += column(prefix.toUpperCase(), 8) + " "; | ||
if (prefix != null && prefix.trim() !== "") | ||
out += chalk_1.default.gray(prefix + ": "); | ||
out += parseMessage(message); | ||
let stdio = ["error", "warn"].indexOf(level) != -1 ? process.stderr : process.stdout; | ||
return new Promise((resolve) => stdio.write(out + "\n", () => resolve())); | ||
} | ||
function className(type) { | ||
if (type != null && type.name) { | ||
return type.name; | ||
} | ||
if (type != null && type.constructor && type.constructor.name) { | ||
return type.constructor.name; | ||
} | ||
return "Unknown"; | ||
} | ||
logs.className = className; | ||
function parseMessage(msg) { | ||
if (msg instanceof Error || (typeof msg === "object" && msg.message != null)) { | ||
msg = msg.name + ": " + msg.message + "\n" + msg.stack; | ||
} | ||
else if (typeof msg === "object") { | ||
try { | ||
const p = JSON.stringify(msg); | ||
return String(p); | ||
} | ||
catch (err) { | ||
return String(`<Unable to parse error> | ||
class: ${className(err)} | ||
stack: ${err.stack} | ||
`); | ||
} | ||
} | ||
return String(msg); | ||
} | ||
function onUncaughtException(msg) { | ||
error("Uncaught Exception!"); | ||
error(msg); | ||
} | ||
function onUnhandledRejection(reason, promise) { | ||
error("Unhandled Rejection!"); | ||
error(reason); | ||
// error(promise); | ||
} | ||
logs.colors = chalk_1.default; | ||
})(logs || (logs = {})); | ||
exports.default = logs; | ||
const logger_1 = require("./logger"); | ||
__exportStar(require("./models"), exports); | ||
exports.default = logger_1.logs; |
{ | ||
"name": "@ascot/log", | ||
"description": "Logging related tools for the @ascot framework", | ||
"version": "2.0.1", | ||
"license": "See license in LICENSE.md", | ||
"dependencies": { | ||
"chalk": "4.1.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "16.11.26" | ||
} | ||
"name": "@ascot/log", | ||
"description": "Logging related tools for the @ascot framework", | ||
"version": "2.0.10", | ||
"license": "See license in LICENSE.md", | ||
"dependencies": { | ||
"chalk": "4.1.2" | ||
} | ||
} |
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
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
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
11887
0
9
282
1