When writing to the server using Node, it is a bit of a headache to troubleshoot the problem, because unlike in Chrome, we have a direct error message in the dev tool, or we can interrupt the debugging point directly.

We often encounter the problem that I can do it in the test environment, but I can’t do it in the Live environment. Without logs, there is no basis for this kind of problem.

So in this article, we’ll talk about how to log in the Node service.

Github can be viewed in this demo

Quickly create a NEW KOA project

Make sure you have koA2 installed globally:

npm i koa2 -g
Copy the code

Then execute:

Koa2 -e node-log # Create a new project CD node-log NPM I # install dependency NPM run start #Copy the code

The basic services are set up. Visit http://localhost:3000/ to see the following page:

This is a quick way to set up a KOA service. Koa-logger, a library for writing logs, is built into the project. Let’s see what it does first.

koa-logger

This library is relatively simple and records basic information about the request, such as the method, URl, time, and so on. For use in middleware, note: recommended before all middleware, this relates to koA’s Onion model. If it is not the first one, the calculation time will be inaccurate.

var logger = require('koa-logger');
app.use(logger());
Copy the code

When we access the responding resource, the console outputs the corresponding log as follows:

  <-- GET /
GET / - 14
  --> GET / 200 19ms 234b
  <-- GET /stylesheets/style.css
GET /stylesheets/style.css - 1
  --> GET /stylesheets/style.css 200 3ms 111b
  <-- GET /favicon.ico
GET /favicon.ico - 1
  --> GET /favicon.ico 404 1ms -
Copy the code

By default, logs are directly exported to the console via the console, if you need to do something custom to the log, such as writing to a log file. This can be done by something like, I keep track of the time:

app.use(logger((str) = > {
  console.log(new Date() + str)
  // redirect koa logger to other output pipe
  // default is process.stdout(by console.log function)
}))
Copy the code

Results:

Mon Oct 11 2021 19:28:41 GMT+0800 (China Standard Time)  <-- GET /
GET / - 10ms
Mon Oct 11 2021 19:28:41 GMT+0800 (China Standard Time)  --> GET / 200 20ms 226b
Mon Oct 11 2021 19:28:41 GMT+0800 (China Standard Time)  <-- GET /stylesheets/style.css
Mon Oct 11 2021 19:28:41 GMT+0800 (China Standard Time)  --> GET /stylesheets/style.css 200 4ms 111b
Copy the code

koa-log4js

The KOA-Logger is lightweight and exposes a flexible interface. But for practical business use, I personally recommend using KOA-log4JS. The main reasons are as follows:

  • koa-loggerIt appears that only middleware usage is supported, not the ability to report specific logs.
  • There are fewer built-in features. For example, log classification and falling disk.

Koa-log4js wraps a layer of Log4JS to support middleware for KOA logging. Its configuration is consistent with log4JS. So if you use log4JS, it should be consistent.

use

Installation:

npm i --save koa-log4
Copy the code

Use the root directory to create a new folder log. Create a new folder utils and create a new file logger.js. The code is as follows:

const path = require('path');
const log4js = require('koa-log4');
const RUNTIME_PATH = path.resolve(__dirname, '.. / ');
const LOG_PATH = path.join(RUNTIME_PATH, 'log');

log4js.configure({
  // Log output
  appenders: {
    access: {
      type: 'dateFile'.pattern: '-yyyy-MM-dd.log'.// Generate file rules
      alwaysIncludePattern: true.// File names are always separated by date
      encoding: 'utf-8'.filename: path.join(LOG_PATH, 'access.log') // Generate the file name
    },
    application: {
      type: 'dateFile'.pattern: '-yyyy-MM-dd.log'.alwaysIncludePattern: true.encoding: 'utf-8'.filename: path.join(LOG_PATH, 'application.log')},out: {
      type: 'console'}},categories: {
    default: { appenders: [ 'out'].level: 'info' },
    access: { appenders: [ 'access'].level: 'info' },
    application: { appenders: [ 'application'].level: 'all'}}});// getLogger passes parameters specifying the type
exports.accessLogger = () = > log4js.koaLogger(log4js.getLogger('access')); // Log all access levels
exports.logger = log4js.getLogger('application');
Copy the code

Configure is a log4js-node configuration (explained later). The log type is passed to the getLogger function. For example, access is an access-level log.

Then add to app.js:

const { accessLogger, logger } = require('./utils/logger');
app.use(accessLogger())
Copy the code

Add the following to routes/index.js:

+ const { logger } = require('.. /utils/logger')

router.get('/', async (ctx, next) => {
+ logger.info(' I'm home ');await ctx.render('index', { title: 'Hello Koa 2! '})})Copy the code

Refresh and you can see two files output in the log folder:

Respectively recorded:

[2021-10-12T10:43:33.914] [INFO] Access - ::1 -- "GET/HTTP/1.1" "200 226 "" Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, Like Gecko) Chrome/94.0.4606.71 Safari/537.36" [2021-10-12T10:43:34.065] [INFO] Access - ::1 -- "GET Stylesheets/styles.css HTTP/1.1" 200 111 "http://localhost:3000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"Copy the code
[2021-10-12T10:43:33.902] [INFO] Application -Copy the code

Let’s look at the log4JS configuration.

level

Log classification can better display logs (in different colors) and selectively drop logs. For example, sensitive debug logs cannot be leaked during production. Log4js has nine levels by default (which you can change with levels), as follows:

{
  ALL: new Level(Number.MIN_VALUE, "ALL"),
  TRACE: new Level(5000."TRACE"),
  DEBUG: new Level(10000."DEBUG"),
  INFO: new Level(20000."INFO"),
  WARN: new Level(30000."WARN"),
  ERROR: new Level(40000."ERROR"),
  FATAL: new Level(50000."FATAL"),
  MARK: new Level(9007199254740992."MARK"), / / 2 ^ 53
  OFF: new Level(Number.MAX_VALUE, "OFF")}Copy the code

As shown below:

Only logs of the same or higher level are generated. For example, if you configure WARN, INFO will not be logged. You can configure log levels for different types of logs in categories configured below.

categories

Log type. The default logging category must be configured for bottom-pocket behavior in the case of no hits. This configuration is an object, and the key value is the class name. For example, in the demo above:

{
  default: { appenders: [ 'out'].level: 'info' },
  access: { appenders: [ 'access'].level: 'info' },
  application: { appenders: [ 'application'].level: 'all'}}Copy the code

Appenders is an array of strings that are output configurations (more on that later), and you can specify more than one. Level is the previous log level.

appenders

After the log classification and classification is solved, the next step is the log falling disk, that is, the problem of outputting logs. The corresponding configuration is appenders, whose key value is a custom name (which can be used by appenders in Categories) and property value is an object that configures the output type. As follows:

// Log output
appenders: {
  access: {
    type: 'dateFile'.pattern: '-yyyy-MM-dd.log'.// Generate file rules
    alwaysIncludePattern: true.// File names are always separated by date
    encoding: 'utf-8'.filename: path.join(LOG_PATH, 'access.log') // Generate the file name
  },
  out: {
    type: 'console'}}Copy the code

In this case, out refers to the output through the console, which can be used as a back pocket for us. In Access, type is dataFile, which refers to the output file, and then the file name and output path are configured. In addition to this type, details can be found on the official website, such as SMTP mail (this requires nodemailer).

Summarizes the configuration

The configuration relationships between log classification, log classification, and log drop disk are as follows:

conclusion

Logs are very important for troubleshooting and locating faults on the server. This article explains how to report logs through the KOA-Logger and KOA-log4js.

The KOA-Logger is lightweight, records basic information about requests, and provides some customization capabilities.

Koa-log4js provides some capabilities in log classification, log classification, and log drop. I think this is more suitable for production environment.