-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathsubject.ts
More file actions
147 lines (127 loc) · 4.25 KB
/
subject.ts
File metadata and controls
147 lines (127 loc) · 4.25 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
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
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import winston from 'winston';
import Transport from 'winston-transport';
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0.0',
environment: 'test',
enableLogs: true,
transport: loggingTransport,
debug: true,
});
async function run(): Promise<void> {
// Create a custom transport that extends winston-transport
const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport);
// Create logger with default levels
const logger = winston.createLogger({
transports: [new SentryWinstonTransport()],
});
// Test basic logging
logger.info('Test info message');
logger.error('Test error message');
// If custom levels are requested
if (process.env.CUSTOM_LEVELS === 'true') {
const customLevels = {
levels: {
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6,
},
colors: {
error: 'red',
warn: 'yellow',
info: 'green',
http: 'magenta',
verbose: 'cyan',
debug: 'blue',
silly: 'grey',
},
};
const customLogger = winston.createLogger({
levels: customLevels.levels,
transports: [new SentryWinstonTransport()],
});
customLogger.info('Test info message');
customLogger.error('Test error message');
}
// If metadata is requested
if (process.env.WITH_METADATA === 'true') {
logger.info('Test message with metadata', {
foo: 'bar',
number: 42,
});
}
if (process.env.WITH_FILTER === 'true') {
const FilteredSentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport, {
levels: ['error'],
});
const filteredLogger = winston.createLogger({
transports: [new FilteredSentryWinstonTransport()],
});
filteredLogger.info('Ignored message');
filteredLogger.error('Test error message');
}
// If unmapped custom level is requested (tests debug line for unknown levels)
if (process.env.UNMAPPED_CUSTOM_LEVEL === 'true') {
const customLevels = {
levels: {
myUnknownLevel: 0,
error: 1,
},
};
// Create transport WITHOUT customLevelMap for myUnknownLevel
// myUnknownLevel will default to 'info', but we only capture 'error'
const UnmappedSentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport, {
levels: ['error'],
});
const unmappedLogger = winston.createLogger({
levels: customLevels.levels,
level: 'error',
transports: [new UnmappedSentryWinstonTransport()],
});
// This should NOT be captured (unknown level defaults to 'info', which is not in levels)
// @ts-ignore - custom levels are not part of the winston logger
unmappedLogger.myUnknownLevel('This unknown level message should be skipped');
// This SHOULD be captured
unmappedLogger.error('This error message should be captured');
}
// If custom level mapping is requested
if (process.env.CUSTOM_LEVEL_MAPPING === 'true') {
const customLevels = {
levels: {
customCritical: 0,
customWarning: 1,
customNotice: 2,
},
};
const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport, {
customLevelMap: {
customCritical: 'fatal',
customWarning: 'warn',
customNotice: 'info',
},
});
const mappedLogger = winston.createLogger({
levels: customLevels.levels,
// https://github.com/winstonjs/winston/issues/1491
// when custom levels are set with a transport,
// the level must be set on the logger
level: 'customNotice',
transports: [new SentryWinstonTransport()],
});
// @ts-ignore - custom levels are not part of the winston logger
mappedLogger.customCritical('This is a critical message');
// @ts-ignore - custom levels are not part of the winston logger
mappedLogger.customWarning('This is a warning message');
// @ts-ignore - custom levels are not part of the winston logger
mappedLogger.customNotice('This is a notice message');
}
await Sentry.flush();
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
void run();