@@ -12,6 +12,8 @@ require('winston-daily-rotate-file');
12
12
import { mapStackTrace } from "../app_transformer/source_maps" ;
13
13
import { pojso } from "../common/util" ;
14
14
import { current } from "./current" ;
15
+ import { PluginInstance } from "./plugin_instance" ;
16
+ import { HyperWrapper } from "./managed_apps" ;
15
17
16
18
export const CONSOLE_METHODS = [ 'debug' , 'info' , 'warn' , 'error' , 'dir' ] as const ;
17
19
export type ConsoleMethod = TupleToUnion < typeof CONSOLE_METHODS >
@@ -31,8 +33,8 @@ const NUMERIC_LOG_LEVELS = {
31
33
32
34
export const ORIGINAL_CONSOLE = { ...console } ;
33
35
34
- export function colorLogLevel ( level : ConsoleMethod | string ) {
35
- const nlevel = level . toLowerCase ( ) ;
36
+ export function colorLogLevel ( level : ConsoleMethod | string , clevel = level ) {
37
+ const nlevel = clevel . toLowerCase ( ) ;
36
38
if ( nlevel == "error" ) return chalk . red ( level ) ;
37
39
if ( nlevel == "warn" ) return chalk . yellow ( level ) ;
38
40
if ( nlevel == "info" ) return chalk . blue ( level ) ;
@@ -71,9 +73,11 @@ export function cleanAndMapStacktrace(err: Error) {
71
73
const fmt = winston . format ;
72
74
73
75
function formatMessage ( m : winston . Logform . TransformableInfo ) {
74
- let { level, message, label } = m ;
75
- level = level . toUpperCase ( ) ;
76
- return chalk `[${ label } ] - ${ colorLogLevel ( level ) } - ${ message } ` ;
76
+ let { level, message, label, level_prefix, label_format } = m ;
77
+ label = ( label_format as string || "[%MSG%]" ) . replace ( '%MSG%' , label ) ;
78
+ let flevel = ( level_prefix || '' ) + level ;
79
+ flevel = flevel . toUpperCase ( ) ;
80
+ return chalk `${ label } - ${ colorLogLevel ( flevel , level ) } - ${ message } ` ;
77
81
}
78
82
79
83
const formatter = fmt . printf ( formatMessage )
@@ -101,6 +105,8 @@ const filter = fmt((info) => {
101
105
} )
102
106
103
107
export interface LoggerOptions {
108
+ level_prefix ?: string ;
109
+ label_format ?: string ;
104
110
domain ?: string ;
105
111
level ?: LogLevel ;
106
112
file ?: string ;
@@ -155,7 +161,11 @@ export function createDomainLogger(opts: LoggerOptions) {
155
161
fmt . label ( { label : opts . domain || 'System' } ) ,
156
162
filter ( ) ,
157
163
) ,
158
- defaultMeta : { service : 'user-service' } ,
164
+ defaultMeta : {
165
+ service : 'user-service' ,
166
+ level_prefix : opts . level_prefix ,
167
+ label_format : opts . label_format ,
168
+ } ,
159
169
transports,
160
170
} ) as any as ExtendedLoger ;
161
171
@@ -204,33 +214,48 @@ export function createDomainLogger(opts: LoggerOptions) {
204
214
205
215
export type LogAMessage = typeof logMessage ;
206
216
217
+ export function logClientMessage ( level : LogLevel , ...rest : any [ ] ) {
218
+ const ctx = current . application ;
219
+ const logger = ctx ?. userSpaceLogger || UNKNOWN_LOGGER
220
+ logger . logMessage ( level , rest ) ;
221
+ }
222
+
223
+ export function logPluginClientMessage ( plugin : any , level : LogLevel , ...rest : any [ ] ) {
224
+ if ( ! ( plugin instanceof PluginInstance ) ) plugin = plugin [ HyperWrapper ] ;
225
+
226
+ const ctx = current . application ;
227
+ const logger = ctx ?. logger || UNKNOWN_LOGGER
228
+ logger . logMessage ( level , [ "[" + chalk . blueBright `${ plugin . id } ` + "]" , ...rest ] , { } ) ;
229
+ }
230
+
207
231
export function logMessage ( level : LogLevel , ...rest : any [ ] ) {
208
232
const ctx = current . application || current . hypervisor ;
209
- const logger = ctx ?. logger || UKNOWN_LOGGER
233
+ const logger = ctx ?. logger || UNKNOWN_LOGGER
210
234
logger . logMessage ( level , rest ) ;
211
235
}
212
236
213
- export function logSystemMessage ( level : LogLevel , ...rest : any [ ] ) {
237
+ export function logHVMessage ( level : LogLevel , ...rest : any [ ] ) {
214
238
const ctx = current . hypervisor ;
215
- const logger = ctx ?. logger || UKNOWN_LOGGER
239
+ const logger = ctx ?. logger || UNKNOWN_LOGGER
216
240
logger . logMessage ( level , rest ) ;
217
241
}
218
242
219
- let UKNOWN_LOGGER = createDomainLogger ( { domain : chalk . yellow ( "???" ) } ) ;
243
+ export let UNKNOWN_LOGGER = createDomainLogger ( { domain : chalk . yellow ( "???" ) } ) ;
220
244
export function setFallbackLogger ( logger : ExtendedLoger ) {
221
- UKNOWN_LOGGER = logger ;
245
+ UNKNOWN_LOGGER = logger ;
222
246
}
223
247
224
248
export function redirectConsole ( ) {
225
249
for ( let cm of CONSOLE_METHODS ) {
226
250
console [ cm ] = ( m , ...rest ) => {
227
251
const ctx = current . application || current . hypervisor ;
228
- const logger = ctx ?. logger || UKNOWN_LOGGER
252
+ const logger = ctx ?. logger || UNKNOWN_LOGGER
229
253
try {
230
254
logger . logMessage ( cm , [ m , ...rest ] ) ;
231
255
} catch ( ex ) {
232
256
ORIGINAL_CONSOLE . error ( ex )
233
257
}
234
258
}
235
259
}
260
+ console . log = ( ...rest ) => console . info ( ...rest ) ;
236
261
}
0 commit comments