-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (87 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
const winston = require('winston');
require('winston-daily-rotate-file');
const Joi = require('joi');
const merge = require('deepmerge');
const { mkdir } = require('fs');
const { join } = require('path');
const auth = require('./lib/auth-jwt');
const logsQuery = require('./lib/logs-query');
const pkg = require('./package.json');
const Schema = require('./validate');
const defaultLogPath = join(process.cwd(), 'logs');
const defaultOptions = {
level: 'debug',
fileLogger: {
logDirectory: defaultLogPath,
filename: 'app.log',
datePattern: 'yyyy-MM-dd.',
prepend: true,
zippedArchive: true,
maxDays: 7,
},
queryApi: {
active: false,
path: '/query-logs',
user: 'query_user',
password: 'password',
security: {
jwt: {
algorithm: 'HS512',
cookieOptions: {
ttl: 1000 * 60 * 60 * 6, // valid for 6 hours
isSecure: true,
isHttpOnly: true,
encoding: 'none',
clearInvalid: false,
strictHeader: true,
},
},
},
},
};
exports.register = (server, options, next) => {
let opts = {};
if (typeof options !== 'undefined') {
opts = options;
}
const validation = Joi.validate(opts, Schema.options);
if (!validation || validation.error) {
throw validation.error;
}
const mergedOpts = merge(defaultOptions, opts || {});
mergedOpts.fileLogger.timestamp = () => new Date().toString();
if (mergedOpts.queryApi.active) {
auth.register(server, mergedOpts.queryApi);
logsQuery.register(server, mergedOpts.queryApi);
}
// Create the log directory.
mkdir(mergedOpts.fileLogger.logDirectory, err => {
if (err) {
// We ignore the error of existing dir.
if (err.code !== 'EEXIST') {
throw err;
}
}
mergedOpts.fileLogger.filename = join(
mergedOpts.fileLogger.logDirectory,
mergedOpts.fileLogger.filename
);
const logger = new winston.Logger({
level: opts.level || defaultOptions.level,
transports: [
new winston.transports.Console({
timestamp() {
return new Date().toString();
},
}),
new winston.transports.DailyRotateFile(mergedOpts.fileLogger),
],
});
server.decorate('request', 'logger', logger);
next();
});
};
exports.register.attributes = {
pkg,
};