/
winston-logger.ts
181 lines (150 loc) · 4.81 KB
/
winston-logger.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* winston-logger.ts
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
import { app } from 'electron';
import winston, { format } from 'winston';
import { Syslog } from 'winston-syslog';
import { Console } from 'winston/lib/winston/transports';
import LogOptions from '../main/log-options';
import { getenv } from './environment';
import { safeError } from './err';
import { Logger, showDiagnosticsOutput } from './logger';
const { combine, printf, timestamp, json } = winston.format;
/**
* A Logger using winston package: https://www.npmjs.com/package/winston
*/
export class WinstonLogger implements Logger {
logger: winston.Logger;
// track file transport so we can close it
fileTransport: winston.transports.FileTransportInstance | null = null;
constructor(logOptions: LogOptions) {
const level = this.readLogLevelOverride(logOptions.getLogLevel());
const format =
logOptions.getLogMessageFormat() === 'pretty'
? printf((info) => `${info.timestamp} ${info.level.toUpperCase()} ${info.message}`)
: combine(removeTimestamp(), json());
const messageFormat = combine(timestamp({ alias: 'time' }), format);
const logFile = logOptions.getLogFile();
let optionError;
this.logger = winston.createLogger({
level: level,
format: messageFormat,
defaultMeta: { service: 'rdesktop' },
});
let consoleLogging = false;
let logTransport;
if (logOptions.getLoggerType() === 'stderr') {
logTransport = new Console();
consoleLogging = true;
}
if (logOptions.getLoggerType() === 'syslog') {
if (process.platform === 'linux') {
logTransport = new Syslog({
protocol: 'unix',
path: '/dev/log',
app_name: 'rdesktop',
});
this.logger.levels = winston.config.syslog.levels;
} else {
optionError = 'syslog not supported';
}
}
if (!logTransport) {
logTransport = new winston.transports.File({
filename: logFile.getAbsolutePath(),
tailable: true,
maxsize: 2000000, // TODO: use max-size from logging.conf (convert from mb to bytes)
maxFiles: 100,
}); // TODO: use max-rotation from logging.conf
this.fileTransport = logTransport;
}
this.logger.add(logTransport);
// on dev builds, always log to console
if (!consoleLogging && !app.isPackaged) {
this.logger.add(new Console());
consoleLogging = true;
}
if (optionError) {
this.logErrorMessage(optionError);
}
}
readLogLevelOverride(defaultLevel: string): string {
const envvars = ['RSTUDIO_DESKTOP_LOG_LEVEL', 'RS_LOG_LEVEL'];
for (const envvar of envvars) {
const envval = getenv(envvar);
if (envval.length !== 0) {
return envval as string;
}
}
return defaultLevel;
}
logLevel(): string {
return this.logger.level;
}
setLogLevel(level: string): void {
this.logger.level = level;
}
log(level: string, message: string): void {
// log to default log locations
this.logger.log(level, message);
}
logError(err: unknown): void {
this.logErrorAtLevel('error', err);
}
logErrorAtLevel(level: string, err: unknown): void {
this.log(level, safeError(err).message);
}
logErrorMessage(message: string): void {
this.log('error', message);
}
logWarning(warning: string): void {
this.log('warn', warning);
}
logInfo(message: string): void {
this.log('info', message);
}
logDebug(message: string): void {
this.log('debug', message);
}
logDiagnostic(message: string): void {
if (showDiagnosticsOutput()) {
console.log(message);
}
}
logDiagnosticEnvVar(name: string): void {
if (showDiagnosticsOutput()) {
const value = getenv(name);
if (value) {
this.logDiagnostic(` . ${name} = ${value}`);
}
}
}
closeLogFile(): void {
if (this.fileTransport) {
this.logger.remove(this.fileTransport);
this.fileTransport = null;
}
}
ensureTransport(): void {
// Winston emits warnings if there is no transport
if (this.logger.transports.length === 0) {
this.logger.add(new Console());
}
}
}
// removes `timestamp` key from json since we add `time` to match Qt log format
const removeTimestamp = format((info) => {
delete info.timestamp;
return info;
});