UNPKG

miniprogram-logger

Version:

It is used to log and gather statistics of users' behavior.

108 lines (107 loc) 3 kB
export class ConsoleManager { constructor(c) { this.Telemetry = []; this.Counters = c ? [c] : []; this.Loggers = c ? [c] : []; this.Timers = c ? [c] : []; } /** * 计数 * @param label * @param value */ count(label, value) { if (this.Counters) { const args = arguments; this.Counters.forEach(v => v.count.apply(v, args)); } } debug() { if (this.Loggers) { const args = arguments; this.Loggers.forEach(v => v.debug.apply(v, args)); } } info() { if (this.Loggers) { const args = arguments; this.Loggers.forEach(v => v.info.apply(v, args)); } } warn() { if (this.Loggers) { const args = arguments; this.Loggers.forEach(v => v.warn.apply(v, args)); } } error() { if (this.Loggers) { const args = arguments; this.Loggers.forEach(v => v.error.apply(v, args)); } } /** * * @param level 日志级别 * @param action 日志操作 * @param content 内容 * @param correlationId 关联ID * @param optionalParams ... 其它参数 */ log(level, action, content, correlationId, ...optionalParams) { const args = arguments; if (level === "time") { if (this.Timers) { this.Timers.forEach(v => v.log.apply(v, Array.prototype.slice.call(args, 1))); } } else if (level === "telemetry") { if (this.Telemetry) { this.Telemetry.forEach(v => v.record.apply(v, Array.prototype.slice.call(args, 1))); } } else if (this.Loggers) { this.Loggers.forEach(v => v.log.apply(v, args)); } } telemetry() { if (this.Telemetry) { const args = arguments; this.Telemetry.forEach(v => v.record.apply(v, args)); } } timeLog() { if (this.Timers) { const args = arguments; this.Timers.forEach(v => v.log.apply(v, args)); } } /** * 开始计时 * @param label - 计时标签 * @param context - 保存上下文信息 */ time(label, context) { if (this.Timers) { this.Timers.forEach(v => v.time(label, context)); } } /** * 结束计时 * @param label - 计时标签 * @param context - 保存上下文信息 */ timeEnd(label, context) { if (this.Timers) { this.Timers.forEach(v => v.timeEnd(label, context)); } } /** * 设置统一的上下文信息 */ setContext(key, value) { this.Loggers.forEach(v => v.setContext && v.setContext(key, value)); this.Telemetry.forEach(v => v.setContext && v.setContext(key, value)); this.Timers.forEach(v => v.setContext && v.setContext(key, value)); } }