Polemo-logger Analysis

Source: Internet
Author: User

Polemo-logger is the log system interface used by the Netease game framework polemo. It encapsulates the log4js-node and has its weakening and enhancement. Detailed description:

  • Enhancement
    • Support for multiple prefixes
    • Row number support
    • Prefix and line number Color support
    • The value sources of the configuration item include environment variables, command line parameters, and optional values.
  • Weakened part (or restricted part)
    • The change support of appender is removed from the loading function of the scheduled configuration file, and only the dynamic loading of level and replaceconsole is retained.
    • Reloadsecs moved from opts to config
    • Greatly reduce module export functions

 

I. Multi-prefix, row number, and color display

Generally, the log message format for each line is as follows.

[2010-01-17 11:43:37.987] [DEBUG] [default] - Some debug messages
  • Debug is the priority
  • Default is category, category

Polemo-logger adds multiple prefixes, such:

[14:29:39. 193] [info] Log-[/users/XXX/pomelo-logger/examples/log. js] [1334] test1

[/Users/pomelo-logger/examples/log. js] and [1334] are prefixes. The prefix format can be 0 or 1 to multiple.

 

Multiple prefix call methods are supported, such:

var logger = require(‘pomelo-logger‘).getLogger(‘log‘, __filename, process.pid);

 

Implementation Method:

  1. In getlogger, after obtaining the logger of log4js, redefine each log function, such as log and error.
  2. Color support for prefix, row number, prefix, and row number

 

Implementation of redefined log functions:

  1. If catagoryname is the file name, remove the current directory prefix.
  2. Initialize the prefix variable as an empty string
  3. If the original mode (process. Env. raw_message is true, this environment variable is also extended by polemo-logger), directly call the log function corresponding to the original log4js to jump to 6.
  4. Otherwise, if the prefix parameter list exists, construct the parameter list string, for example, "[a] [B]", and assign a value to the prefix.
  5. If the parameter list exists and the row number (process. Env. logger_line) needs to be displayed, add the row number in the prefix, for example, "4 :"
  6. Call the corresponding log functions of log4js
  7. Return

 

2. Support for row numbers

Tips are used to correctly display the row number.

 

  1. Construct an error class
  2. Split the stack members by pressing the carriage return and take the fourth row.
  3. Split by colon, take the second

 

After var E = new error ();, print the stack of error as follows:

 

Error

At Getline (/users/XXX/pomelo-logger/lib/logger. JS: 243: 10)

At object. plogger. (anonymous function) [as info] (/users/XXX/pomelo-logger/lib/logger. JS: 39: 10)

At object. <anonymous> (/users/XXX/pomelo-logger/examples/log. JS: 4: 8)

At module. _ compile (module. JS: 456: 26)

At object. Module. _ extensions.. js (module. JS: 474: 10)

At module. Load (module. JS: 356: 32)

At function. Module. _ load (module. JS: 312: 12)

At function. Module. runmain (module. JS: 497: 10)

At startup (node. JS: 119: 16)

At node. JS: 906: 3

 

3. Color support

Polemo-logger uses the same color as the priority for the prefix and row number. Use ASCII control codes to display foreground colors. The console of Linux and MacOSX supports it.

 

Implementation Method:

  1. Copy the styles variable from log4js to indicate the ASCII escape of the current style.
  2. Copy the colours variable from log4js to indicate the style corresponding to different priorities.
  3. Copy the colorizestart, colorizeend, and colorize functions from log4js. They receive strings and style parameters and return the processed strings.
  4. Obtains the corresponding style based on the current log priority, and CALLS colorize to obtain a string with color support.

 

Iv. value source extension of configuration items

The Configure function is responsible for configuration (config, file or object) and option (OPTs) settings of the log system.

Polemo-logger extends the value of configuration items in the configure function, and supports environment variables (process. ENV), command line parameters (process. argv) and opts ).

This extension is triggered when the value type of the configuration item is string and the format is {scope: Arg-name.

Valid values of scope: ENV, argS, and opts.

Arg-name indicates the name of the key.

 

V. reloadsecs

Reloadsecs indicates the time interval for automatically reload the configuration file, in seconds. If it is 0, it will not be automatically loaded.

The Configure function calls the reloaded initialization function initreloadconfiguration, which is the parameter required to initialize the reload function.

The setinterval function is called in initreloadconfiguration to regularly reload the reloadconfiguration.

This reloadconfiguration function checks and records the last modification time of the configuration file, compares the two files, and loads the configuration file only once after each change.

The configure of polemo-logger will eventually call this function of log4js.

From configure function to reload related functions, polemo-logger changes log4js:

  • In the configure function, extend the value source of the configuration item
  • The Configure function supports custom linedebug (whether the row number is displayed) and config. rawmessage (whether the prefix and row number are not displayed)
  • In the configure function, read reloadsecs and call initreloadconfiguration
  • In the configureonceoff function (the function called by reloadconfiguration, that is, the function for applying and setting specific configuration items), the setting of appenders (such as file and console) is removed.

Reloadsecs moved from opts to config (configuration file or object), probably to enable the time interval for Automatic Reload to exist in the form of configuration items.

In addition to the appenders application and settings in the reload function, the intent is unclear or the specific meaning is unclear. The cost is to introduce (copy) from log4js) six functions.

 

6. Module Export (module. Exports)

Because polemo-logger is the encapsulation of log4js, its export is the functional limitation expected by the user. According to the table below, polemo-logger is a subset of log4js and has made minor functional improvements and restrictions.

 

Category Name Implementation location
Logger Getlogger Polemo-logger
  Getdefaloglogger Log4js
Configuration Configure Polemo-logger
Appender Addappender Log4js
  Loadappender Log4js
  Clearappenders Log4js
  Appenders Log4js
Console Replaceconsole Log4js
  Restoreconsole Log4js
Priority Levels Log4js
  Setgloballoglevel Log4js
Layout Layouts Log4js

 

VII. Summary

I feel that the enhancement is significant, and the removal of appender in the weakening part is of little significance, unless the related code of log4js has bugs.

 

VIII. Reference:

Https://github.com/NetEase/pomelo-logger/

Https://github.com/nomiddlename/log4js-node/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.