1.1.3 • Published 5 years ago

log4js-meili-appender v1.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

log4js meili appender

use in koa

import log4js from 'koa-log4';

// filter setup
app.use(log4js.koaLogger(log4js.getLogger('meili'), {
  format: function(ctx, func) {
    return func(`{
      "userId": ":user-id",
      "userType": 2,
      "userNick": ":user-nick",
      "traceId": ":trace-id",
      "objectIds": "",
      "objectType": "",
      "operateType": "",
      "beforeData": "",
      "afterData": "",
      "remarks": "",
      "userIp": ":user-ip",
      "userAgent": ":user-agent",
      "referer": ":referrer",
      "url": ":url",
      "uri": ":uri",
      "serverIp": ":server-ip",
      "method": ":method",
      "requestParams": ":requestParams"
    }`);
  },
  level: 'auto',
  nolog: '\\.(gif|jpe?g|png)$',
  tokens: [
    // 其他信息
    { token: ':requestParams', content: (ctx) => {
      let content = {};
      if (ctx.method === 'GET') {
        content = ctx.query;
      } else {
        content = ctx.request.body;
      }
      return JSON.stringify(content).replace(/"/g, '\\"');
    } },
    { token: ':user-id', content: (ctx) => { return ctx.state.userInfo.work_id || ''; } },
    { token: ':user-nick', content: (ctx) => { return ctx.state.userInfo.nick_name || ''; } },
    { token: ':user-ip', content: (ctx) => { return ctx.ip; } },
    { token: ':uri', content: (ctx) => { return ctx.path; } },
    { token: ':method', content: (ctx) => { return ctx.method; } },
    { token: ':server-ip', replacement: ip.address() },
    { token: ':trace-id', content: (ctx) => { return ctx.state.request_id || ''; } },
  ],
}));

// appender setup
// get appName and secretKey from
// http://crm.mogujie.org/actionlog/admin/index
// @see http://gitlab.mogujie.org/yefei/action-log
let appName = '******';
let secretKey = '******';
log4js.configure({
  appenders: [
    {
      type: 'log4js-meili-appender',
      category: 'meili',
      appName,
      secretKey,
    },
  ],
});
1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago