|
| 1 | +/** |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as consoleLogLevel from 'console-log-level'; |
| 18 | +import {defaultConfig} from './config'; |
| 19 | + |
| 20 | +export type ConsoleLogLevel = 'error'|'warn'|'info'|'debug'; |
| 21 | +export type LogLevel = 'silent'|ConsoleLogLevel; |
| 22 | + |
| 23 | +/** |
| 24 | + * The list of log levels. |
| 25 | + */ |
| 26 | +export const LEVELS: ReadonlyArray<LogLevel> = |
| 27 | + ['silent', 'error', 'warn', 'info', 'debug']; |
| 28 | + |
| 29 | +export interface LoggerConfig { |
| 30 | + /** |
| 31 | + * The minimum log level that will print to the console. |
| 32 | + */ |
| 33 | + level: string|false; |
| 34 | + |
| 35 | + /** |
| 36 | + * A tag to use in log messages. |
| 37 | + */ |
| 38 | + tag: string; |
| 39 | +} |
| 40 | + |
| 41 | +function logLevelToName(level?: number): LogLevel { |
| 42 | + if (typeof level === 'string') { |
| 43 | + level = Number(level); |
| 44 | + } |
| 45 | + if (typeof level !== 'number') { |
| 46 | + level = defaultConfig.logLevel; |
| 47 | + } |
| 48 | + if (level < 0) level = 0; |
| 49 | + if (level >= LEVELS.length) level = LEVELS.length - 1; |
| 50 | + return LEVELS[level]; |
| 51 | +} |
| 52 | + |
| 53 | +export class Logger { |
| 54 | + private logger: consoleLogLevel.Logger|null; |
| 55 | + |
| 56 | + constructor(opts?: Partial<LoggerConfig>) { |
| 57 | + const levelName = opts && opts.level !== undefined ? |
| 58 | + opts.level : |
| 59 | + logLevelToName(defaultConfig.logLevel); |
| 60 | + |
| 61 | + if (levelName === false || levelName === 'silent') { |
| 62 | + this.logger = null; |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + this.logger = consoleLogLevel({ |
| 67 | + stderr: true, |
| 68 | + prefix: opts && opts.tag ? opts.tag : 'unknown', |
| 69 | + level: levelName as ConsoleLogLevel |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + error(...args: Array<{}>): void { |
| 74 | + if (this.logger) { |
| 75 | + this.logger.error(...args); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + warn(...args: Array<{}>): void { |
| 80 | + if (this.logger) { |
| 81 | + this.logger.warn(...args); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + debug(...args: Array<{}>): void { |
| 86 | + if (this.logger) { |
| 87 | + this.logger.debug(...args); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + info(...args: Array<{}>): void { |
| 92 | + if (this.logger) { |
| 93 | + this.logger.info(...args); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments