forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatters.ts
More file actions
86 lines (76 loc) · 2.92 KB
/
formatters.ts
File metadata and controls
86 lines (76 loc) · 2.92 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { format } from 'winston';
import { isTestExecution } from '../common/constants';
import { getLevel, LogLevel, LogLevelName } from './levels';
const TIMESTAMP = 'YYYY-MM-DD HH:mm:ss';
// Knobs used when creating a formatter.
type FormatterOptions = {
label?: string;
};
// Pascal casing is used so log files get highlighted when viewing
// in VSC and other editors.
const formattedLogLevels: { [K in LogLevel]: string } = {
[LogLevel.Error]: 'Error',
[LogLevel.Warn]: 'Warn',
[LogLevel.Info]: 'Info',
[LogLevel.Debug]: 'Debug',
[LogLevel.Trace]: 'Trace',
};
// Return a consistent representation of the given log level.
function normalizeLevel(name: LogLevelName): string {
const level = getLevel(name);
if (level) {
const norm = formattedLogLevels[level];
if (norm) {
return norm;
}
}
return `${name.substring(0, 1).toUpperCase()}${name.substring(1).toLowerCase()}`;
}
// Return a log entry that can be emitted as-is.
function formatMessage(level: LogLevelName, timestamp: string, message: string): string {
const levelFormatted = normalizeLevel(level);
return isTestExecution()
? `${process.pid} ${levelFormatted} ${timestamp}: ${message}`
: `${levelFormatted} ${timestamp}: ${message}`;
}
// Return a log entry that can be emitted as-is.
function formatLabeledMessage(level: LogLevelName, timestamp: string, label: string, message: string): string {
const levelFormatted = normalizeLevel(level);
return isTestExecution()
? `${process.pid} ${levelFormatted} ${label} ${timestamp}: ${message}`
: `${levelFormatted} ${label} ${timestamp}: ${message}`;
}
// Return a minimal format object that can be used with a "winston"
// logging transport.
function getMinimalFormatter() {
return format.combine(
format.timestamp({ format: TIMESTAMP }),
format.printf(
// This relies on the timestamp formatter we added above:
({ level, message, timestamp }) => formatMessage(level as LogLevelName, timestamp, message),
),
);
}
// Return a minimal format object that can be used with a "winston"
// logging transport.
function getLabeledFormatter(label_: string) {
return format.combine(
format.label({ label: label_ }),
format.timestamp({ format: TIMESTAMP }),
format.printf(
// This relies on the label and timestamp formatters we added above:
({ level, message, label, timestamp }) =>
formatLabeledMessage(level as LogLevelName, timestamp, label, message),
),
);
}
// Return a format object that can be used with a "winston" logging transport.
export function getFormatter(opts: FormatterOptions = {}) {
if (opts.label) {
return getLabeledFormatter(opts.label);
}
return getMinimalFormatter();
}