-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtransport.builder.ts
103 lines (91 loc) · 3.59 KB
/
transport.builder.ts
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
102
103
import { TransportInstance } from 'winston';
import * as winston from 'winston';
import { ConsoleTransport, DailyRotateFile, FileTransport } from './interfaces/transport.interface';
import { resolve } from 'path';
import * as fs from 'fs';
import * as mkdirp from 'mkdirp';
import moment = require('moment');
export class TransportBuilder {
private readonly AVAILABLE_TRANSPORTS = ['console', 'file', 'dailyRotateFile'];
constructor(
private readonly basePath: string,
) {
}
public build(transports: (DailyRotateFile | ConsoleTransport | FileTransport)[]): TransportInstance[] {
const instances: TransportInstance[] = transports.filter(item => this.AVAILABLE_TRANSPORTS.includes(item.transport))
.map(item => {
switch (item.transport) {
case 'file':
return this.buildFileTransportInstance(item as FileTransport);
case 'dailyRotateFile':
return this.buildDailyRotateFileTransportInstance(item as DailyRotateFile);
case 'console':
default:
return this.buildConsoleTransportInstance(item as ConsoleTransport);
}
});
if (instances.length === 0) {
instances.push(this.buildDefaultTransport());
}
return instances;
}
private mkdirPath(filename) {
if (filename.charAt(0) !== '/') {
filename = resolve(this.basePath, filename);
}
const last = filename.lastIndexOf('/');
const path = filename.substring(0, last);
if (!fs.existsSync(path)) {
mkdirp.sync(path);
}
return filename;
}
private buildConsoleTransportInstance(config: ConsoleTransport) {
return new winston.transports.Console({
colorize: config.colorize,
label: config.label,
timestamp: () =>
moment(new Date().getTime()).format(config.datePattern || 'YYYY-MM-DD h:mm:ss'),
});
}
private buildFileTransportInstance(config: FileTransport) {
return new winston.transports.File({
name: config.name,
level: config.level,
filename: this.mkdirPath(config.filename),
maxsize: config.maxSize,
label: config.label,
maxFiles: config.maxFiles,
json: config.json,
eol: config.eol,
zippedArchive: config.zippedArchive,
silent: config.silent,
colorize: config.colorize,
prettyPrint: config.prettyPrint,
logstash: config.logstash,
depth: config.depth,
tailable: config.tailable,
maxRetries: config.maxRetries,
options: config.options,
timestamp: () =>
moment(new Date().getTime()).format(config.datePattern || 'YYYY-MM-DD h:mm:ss'),
});
}
private buildDailyRotateFileTransportInstance(config: DailyRotateFile) {
return new winston.transports.DailyRotateFile({
filename: this.mkdirPath(config.filename),
datePattern: config.datePattern || 'YYYY-MM-DD h:mm:ss',
zippedArchive: config.zippedArchive,
maxSize: config.maxSize,
maxFiles: config.maxFiles,
options: config.options,
});
}
private buildDefaultTransport() {
return new winston.transports.Console({
colorize: true,
label: '',
timestamp: () => moment(new Date().getTime()).format('YYYY-MM-DD h:mm:ss'),
});
}
}