In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to build node services to achieve log processing, I hope you will gain something after reading this article, let's discuss it together!
I. Technology selection
Three mainstream technologies are selected for comparison:
1.1 log4jslog4js is a node log management tool that can export logs in custom formats to various channels. For the log output of the console, the color log can be presented, and for the log output of file mode, the log can be cut according to the file size or date. Developers familiar with java will find that log4js is very similar to log4j, a commonly used java logging tool. Yes, log4js is the JavaScript version of log4j and is used in a similar way. 1.2 winstonwinston is also a very popular node log management tool that supports multiple transfers. The default output format is json, or you can customize the output format. If you want to cut the log, you also need to use the winston-daily-rotate-file module. 1.3 PM2PM2 is actually a node process management tool, with performance monitoring, process daemon, load balancing, log management and other functions. Using PM2 for log management requires only the addition of console method calls to the project, without the need to add additional code. To cut the log, you need to use pm2-logrotate. Since many of the server-side systems within the team are based on java, most of these systems use log4j to generate logs. Log management related log collection system and log query system support the log in log4j format better, so I finally choose the JavaScript version of log4j log4js to generate logs, the following will introduce the basic use of log4js.
II. Basic concepts
2.1 Log level
The default log level of log4js is divided into 9 levels, listed in order of priority from lowest to highest: ALL
< TRACE < DEBUG < INFO < WARN < ERROR < FATAL < MARK < OFF当日志级别为ALL时,会输出所有级别的日志当日志级别为OFF时,则会关闭日志,不会有任何日志输出用户还可以根据自己的需要自定义日志级别2.2 appenderappender 主要是用来定义以怎样的方式输出,输出到哪里。可以将日志写入到文件、发送电子邮件、通过网络发送数据等。可以通过配置对象的appenders属性定义多个appender。appender的常用类型有:console:控制台输出file:文件输出dateFile:按日期切割的文件输出2.3 categorycategory 是日志的类型,指定一个或者多个appender为某种类型的日志,不同类型的日志可以指定不同的日志级别。可以通过配置对象的categories属性定义多个category。必须指定default类型,用来获取默认的Logger实例,还可以通过类型名来获取指定类型的Logger实例。综上所诉, appender 定义了日志输出到哪里,category 将appender 进行了分类,不同类型指定不同的日志级别。 三、使用log4js 3.1 安装 npm install log4js --save 或者 yarn add log4js 3.2 简单使用下面示例利用log4js创建日志对象logger,通过调用logger.debug、logger.info、logger.warn、logger.error 等方法将日志输出到控制台和日志文件。util/log4jsLogger.jsconst path = require('path'); const log4js = require('log4js'); // 配置log4js log4js.configure({ appenders: { // 控制台输出 console: { type: 'console' }, // 日志文件 file: { type: 'file', filename: path.join(__dirname, '../../logs/server.log')} }, categories: { // 默认日志 default: { appenders: [ 'file', 'console' ], level: 'debug' }, } }); // 获取默认日志 const logger = log4js.getLogger(); module.exports = logger; server.js 再通过调用logger. info 输出INFO 级别的日志,这里web开发框架使用的Koa。 const Koa = require('koa'); const router = require('./router'); const logger = require('./util/log4jsLogger'); const port = 3000; const app = new Koa() .use(router.routes()) .use(router.allowedMethods()); app.listen(port, () =>{
Logger.info (`Server running on port ${port} `)
}); 3.3.Log format log4js sets log format through layout. Built-in layout includes: basic: basic log format including timestamp, log level and log type colored: format is the same as that of basic, except that different levels of logs display different colors dummy: output only the content of the first parameter No time stamp, log level, log classification and other information pattern: layout example of customizable format: default log format: [2020-04-01T11:33:43.317] [INFO] default-Server running on port 3000 Custom Log format: 2020-04-01 1133 default 317 [INFO] Log 3000 Code: / / Custom Log format
Const layout = {
Type: 'pattern'
Pattern:'% d {yyyy-MM-dd hh:mm:ss.SSS} [% p]% m'
}
Log4js.configure ({
Appenders: {
/ / console output
Console: {type: 'console'}
/ / Log file, which can be formatted by setting layout
File: {type: 'file', filename: path.join (_ _ dirname,'.. /.. / logs/server.log'), layout}
}
Categories: {
/ / default log
Default: {appenders: ['file',' console'], level: 'debug'}
}
(}); 3.4 Log cutting if all logs are output to one file, the log files will become larger and larger, making it very inconvenient to back up and view logs. It is possible to slice logs by date by specifying appender as the dateFile type. / / Log configuration
Log4js.configure ({
Appenders: {
/ / console output
Console: {type: 'console'}
/ / Log file
File: {
Type: 'dateFile'
Filename: path.join (_ _ dirname,'.. /.. / logs/server.log')
/ / File name suffix format after log cutting
Pattern:'. Yyyy-MM-dd'
}
}
Categories: {
/ / default log
Default: {appenders: ['file',' console'], level: 'debug'}
}
}); if the service is deployed on April 1, the log will be output to the service.log file, and on April 2, the service.log will be renamed to server.log.2020-04-01, then a new service.log file will be created, and the new log will continue to be exported to the service.log file. Output multiple files the following example outputs not only the full log to server.log, but also the log at the level of error and above to server-error.log. Util/log4jsLogger.jsconst path = require ('path')
Const log4js = require ('log4js')
/ / configure log4js
Log4js.configure ({
Appenders: {
/ / console output
Console: {type: 'console'}
/ / all log files
AllFile: {type: 'file', filename: path.join (_ _ dirname,'.. / logs/server.log')}
/ / error log file
ErrorFile: {type: 'file', filename: path.join (_ _ dirname,'.. / logs/server-error.log')}
}
Categories: {
/ / default log, which outputs logs at debug level or above
Default: {appenders: ['allFile',' console'], level: 'debug'}
/ / error log, output log at error level or above
Error: {appenders: ['errorFile'], level:' error'}
}
});
/ / get the default log
Const defaultLogger = log4js.getLogger ()
/ / get error level log
Const errorLogger = log4js.getLogger ('error')
/ / Log agent, calling both the default log and the error log
Const loggerProxy = {}
Const levels = log4js.levels.levels
Levels.forEach (level = > {
Const curLevel = level.levelStr.toLowerCase ()
LoggerProxy [curLevel] = (... params) = > {
DefaultLogger [curLevel] (. Params)
ErrorLogger [curLevel] (. Params)
}
});
Module.exports = loggerProxy;3.6 overwrite console because you need to call logger.debug, logger.info, logger.warn, logger.error and other methods to use log4js. For projects that have called the console method to output the log, all the projects that have called the logger method are changed to call the logger method, which is troublesome to change. You can use log4js to output the log by overriding the console method. / * *
* create a log agent method
* @ param logLevel log level
* @ param logger log object
* @ return {function}
, /
Function createLogProxy (logLevel, logger) {
Return (... param) = > {
Logger [logLevel] (. Param)
}
}
Console.log = createLogProxy ('debug', logger)
Console.info = createLogProxy ('info', logger)
Console.warn = createLogProxy ('warn', logger)
Console.error = createLogProxy ('error', logger); to ensure that all logs are output to the log file, get the logger object and override the console method as soon as possible.
After reading this article, I believe you have a certain understanding of "how to build node services to achieve log processing". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.