@7util/logger 中文文档教程

发布于 3年前 浏览 21 项目主页 更新于 3年前

Logger :rocket:

Simple JS logger

支持浏览器、NodeJS 和 TypeScript

$ npm install @7util/logger

Features :loudspeaker:

  • print fully arrays and objects
  • accept multiple arguments
  • trace and error have stack
  • debug level mode
  • have timestamp
  • include TypeScript definitions
  • logs in timestamped files on the disc (NodeJS)

Example

logger.ts

import { Logger, ILogOptions } from '@7util/logger';
// OR 
const { Logger } = require('@7util/logger'); // no options interface available


/** ILogOptions
 * @prop {boolean} useColor? optional -  Should use colors.
 * @prop {string} fileName? optional - The name of the file.
 * @prop {boolean} logInFile? optional - Should write in file on the disc.
 * @prop {string} logLevel? optional - Will output debug logs if value is "debug".
 */

const options: ILogOptions = {
    useColor: true,
    logInFile: true,
    fileName: 'rest-server.log',
    logLevel: process.env.LOG_LEVEL
};

const logger: Logger = new Logger(options);// or simply ...new Logger()

export default logger;

:construction: 在浏览器中使用记录器时,确保激活所有日志级别(详细、信息、警告和错误)。

在你项目的某处……

import logger from './logger.ts'; // your logger instance

const obj = {
    key: 'value1',
    inner: { secret: 'value2' },
    arr: [1, []]
};

logger.info('info', obj, 'msg');
logger.warn('warn', obj, 'msg');
logger.trace('trace', obj, 'msg');
logger.error(obj, new Error('msg'));
logger.debug('debug', obj, 'msg');
logger.success('success', obj, 'msg');

/* output */
[2021-04-08 13:11:40] INFO: info {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
[2021-04-08 13:11:40] WARN: warn {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
[2021-04-08 13:11:40] TRACE:: trace {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
    at Object...
    at Module...
[2021-04-08 13:11:40] ERROR: {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} Error: msg
    at Object...
    at Module...
[2021-04-08 13:11:40] DEBUG: debug {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
[2021-04-08 13:11:40] SUCCESS: success {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg

Happy coding! <:beer:/>

Logger :rocket:

Simple JS logger

Supports Browser, NodeJS and TypeScript

$ npm install @7util/logger

Features :loudspeaker:

  • print fully arrays and objects
  • accept multiple arguments
  • trace and error have stack
  • debug level mode
  • have timestamp
  • include TypeScript definitions
  • logs in timestamped files on the disc (NodeJS)

Example

logger.ts

import { Logger, ILogOptions } from '@7util/logger';
// OR 
const { Logger } = require('@7util/logger'); // no options interface available


/** ILogOptions
 * @prop {boolean} useColor? optional -  Should use colors.
 * @prop {string} fileName? optional - The name of the file.
 * @prop {boolean} logInFile? optional - Should write in file on the disc.
 * @prop {string} logLevel? optional - Will output debug logs if value is "debug".
 */

const options: ILogOptions = {
    useColor: true,
    logInFile: true,
    fileName: 'rest-server.log',
    logLevel: process.env.LOG_LEVEL
};

const logger: Logger = new Logger(options);// or simply ...new Logger()

export default logger;

:construction: When using the logger in the browser make sure that all log levels (verbose, info, warnings and errors) are activated.

somewhere in your project…

import logger from './logger.ts'; // your logger instance

const obj = {
    key: 'value1',
    inner: { secret: 'value2' },
    arr: [1, []]
};

logger.info('info', obj, 'msg');
logger.warn('warn', obj, 'msg');
logger.trace('trace', obj, 'msg');
logger.error(obj, new Error('msg'));
logger.debug('debug', obj, 'msg');
logger.success('success', obj, 'msg');

/* output */
[2021-04-08 13:11:40] INFO: info {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
[2021-04-08 13:11:40] WARN: warn {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
[2021-04-08 13:11:40] TRACE:: trace {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
    at Object...
    at Module...
[2021-04-08 13:11:40] ERROR: {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} Error: msg
    at Object...
    at Module...
[2021-04-08 13:11:40] DEBUG: debug {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg
[2021-04-08 13:11:40] SUCCESS: success {"key":"value1","inner":{"secret":"value2"},"arr":[1,[]]} msg

Happy coding! <:beer:/>

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文