未加星标

Simple Logging Facade for JavaScript

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Logtown

Simple Logging Facade for javascript.

Installation npm install logtown --save

or

yarn add logtown Usage

First of all you should notice, that logtown is not a logger . You can use any logger you want underneath, there are a lot of great tools like intel, winston and others. And if you don't define any wrappers you won't see any output. There are 2 ready for use wrappers contained in the package. But you can use your own in any time.

So let's start from simple use case:

const Logger = require('logtown'); // Adding wrapper is very simple, you need implement at least 1 method from this array ['log', 'silly', 'debug', 'info', 'warn', 'error'] // log method has only 1 difference from others it receives additional *level* parameter Logger.addWrapper({ log(id, level, stats, ...rest) { console[level](`[${id}] ${rest.join(' ')}`) } }) // Once you added wrapper you can use logger in any place of your app const logger = Logger.getLogger('mymodule-label'); // if you prefer factory method then you can use getLogger as: // const logger = require('logtown')('my-namespace'); // or // const logger = Logger('my-namespace'); logger.silly('Silly message'); logger.debug('Debug message'); logger.info('Info message'); logger.warn('Warn message'); logger.error('Error message'); Using in es5 environment

There are several precompiled versions of this module placed in folders es5 and es6 . You might select the one you needed by importing require('logtown/es5/common') for old commonjs environments.

Adding new wrapper

Adding wrapper as you noticed before in the example, pretty simple operation. You required to implement at least one method or pass single function that will work like the most advanced wrapper's function log .

Example,

const Logger = require('logtown'); // passing function as wrapper Logger.addWrapper(function (id, level, stats, ...rest) { console.log(`${level} [${id}]`, ...rest); }); // the same as above Logger.addWrapper({ log: function (id, level, stats, ...rest) { console.log(`${level} [${id}]`, ...rest); } }); // pass only one function for required level Logger.addWrapper({ info: function (id, stats, ...rest) { // log only info messages in this wrapper console.info(...rest); } }); class AdvancedWrapper { constructor(options = {}) { this.supperLogger = new SupperLogger(options); // preparing supper logger ... } log(id, level, stats, ...rest) { this.supperLogger.log(...rest); } } Logger.addWrapper(new AdvancedWrapper({option1: "value"})); Configuration

Logtown can be configured in 3 ways:

by defining application level configurations by defining logger level configurations

and by defining global level configurations

Application level is made by using Logger.configure({}) static method.

const logger = Logger.configure({ namespaces: {'mymodule': {disable: ['debug']}}, // disable one or more level for specific namespace useGlobal: false, // disable usage of global configurations, it is enabled by default disable: ['info', 'warn'] // disable globaly specific levels });

You can set several options also during creation of the new logger.

const logger1 = Logger.getLogger('mymodule', {disable: 'debug'}) const logger2 = Logger.getLogger('mymodule', {disable: ['debug', 'info']})

Each time you getLogger with new configuration, it will be merged with previously defined ones, even though logger instance will be the same.

In very specific case you need to shut down loggers from nested npm modules that you can't affect on. One of the solutions is to use peerDependencies , but if peerDependencies is not the case you can set global configuration object, that will be handled by nested logtown package.

_.set(global, 'logtown.namespaces.module-that-makes-mess-in-logs.disable', ['silly', 'debug']);

Tip: it is easy to use easy-loader to configure logtown for each environment.

const config = require('../config'); const Logger = require('logtown'); Logger.configure(config('logtown', {})) Experimental feature

By default you are limited with defined method set. It gives possibility to rely on logger api anytime and be ready to swap out it when needed. But sometimes you would need to use your logger's very specific functionality. For example, console.group . There are 2 ways of using it - by direct calling console.group method, that is bad and in some level unstable. And second one, to notify somehow your wrapper.

So logtown introduces send method. That is behaves the same as other methods, but gives possibility to pass level .

Logger.addWrapper({ group: function (id, stats, ...rest) { console.group(id); }, groupend: function(id, stats, ...rest) { console.groupEnd(id); } }); const logger = Logger.getLogger('my-namespace'); logger.send('group'); logger.debug('Hello World'); logger.send('groupend');

Important to note that send method won't fallback to log method if it exists in wrapper.

License

Logtown is released under the MIT license.

Donate

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJava
分页:12
转载请注明
本文标题:Simple Logging Facade for JavaScript
本站链接:http://www.codesec.net/view/524065.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(32)