@@ -16,188 +16,188 @@ import { backupInProgress } from "../database/_dbState";
1616const padNewlines = true ; //process.env.PAD_NEW_LINES !== "false";
1717
1818type LogLevel =
19- | "error"
20- | "warn"
21- | "info"
22- | "debug"
23- | "verbose"
24- | "silly"
25- | "task"
26- | "ut" ;
19+ | "error"
20+ | "warn"
21+ | "info"
22+ | "debug"
23+ | "verbose"
24+ | "silly"
25+ | "task"
26+ | "ut" ;
2727
2828// biome-ignore lint/suspicious/noControlCharactersInRegex: <benis>
2929const ansiRegex = / \x1B \[ [ 0 - ? 9 ; ] * [ m G ] / g;
3030
3131const formatTerminalMessage = ( message : string , prefix : string ) : string => {
32- try {
33- const cleanPrefix = prefix . replace ( ansiRegex , "" ) ;
34- const maxWidth = process . stdout . columns || 80 ;
35- const wrapWidth = Math . max ( maxWidth - cleanPrefix . length - 3 , 20 ) ;
36-
37- if ( ! padNewlines ) return message ;
38-
39- const wrapped = wrapAnsi ( message , wrapWidth , {
40- trim : true ,
41- hard : true ,
42- wordWrap : true ,
43- } ) ;
44-
45- return wrapped
46- . split ( "\n" )
47- . map ( ( line , index ) => {
48- return index === 0 ? line : `${ " " . repeat ( cleanPrefix . length ) } ${ line } ` ;
49- } )
50- . join ( "\n" ) ;
51- } catch ( error ) {
52- console . error ( "Error formatting terminal message:" , error ) ;
53- return message ;
54- }
32+ try {
33+ const cleanPrefix = prefix . replace ( ansiRegex , "" ) ;
34+ const maxWidth = process . stdout . columns || 80 ;
35+ const wrapWidth = Math . max ( maxWidth - cleanPrefix . length - 3 , 20 ) ;
36+
37+ if ( ! padNewlines ) return message ;
38+
39+ const wrapped = wrapAnsi ( message , wrapWidth , {
40+ trim : true ,
41+ hard : true ,
42+ wordWrap : true ,
43+ } ) ;
44+
45+ return wrapped
46+ . split ( "\n" )
47+ . map ( ( line , index ) => {
48+ return index === 0 ? line : `${ " " . repeat ( cleanPrefix . length ) } ${ line } ` ;
49+ } )
50+ . join ( "\n" ) ;
51+ } catch ( error ) {
52+ console . error ( "Error formatting terminal message:" , error ) ;
53+ return message ;
54+ }
5555} ;
5656
5757const levelColors : Record < LogLevel | string , ChalkInstance > = {
58- error : chalk . red . bold ,
59- warn : chalk . yellow . bold ,
60- info : chalk . green . bold ,
61- debug : chalk . blue . bold ,
62- verbose : chalk . cyan . bold ,
63- silly : chalk . magenta . bold ,
64- task : chalk . cyan . bold ,
65- ut : chalk . hex ( "#9D00FF" ) ,
58+ error : chalk . red . bold ,
59+ warn : chalk . yellow . bold ,
60+ info : chalk . green . bold ,
61+ debug : chalk . blue . bold ,
62+ verbose : chalk . cyan . bold ,
63+ silly : chalk . magenta . bold ,
64+ task : chalk . cyan . bold ,
65+ ut : chalk . hex ( "#9D00FF" ) ,
6666} ;
6767
6868const parseTimestamp = ( timestamp : string ) : string => {
69- const [ datePart , timePart ] = timestamp . split ( " " ) ;
70- const [ day , month ] = datePart . split ( "/" ) ;
71- const [ hours , minutes , seconds ] = timePart . split ( ":" ) ;
72- const year = new Date ( ) . getFullYear ( ) ;
73- const date = new Date (
74- year ,
75- Number . parseInt ( month ) - 1 ,
76- Number . parseInt ( day ) ,
77- Number . parseInt ( hours ) ,
78- Number . parseInt ( minutes ) ,
79- Number . parseInt ( seconds )
80- ) ;
81- return date . toISOString ( ) ;
69+ const [ datePart , timePart ] = timestamp . split ( " " ) ;
70+ const [ day , month ] = datePart . split ( "/" ) ;
71+ const [ hours , minutes , seconds ] = timePart . split ( ":" ) ;
72+ const year = new Date ( ) . getFullYear ( ) ;
73+ const date = new Date (
74+ year ,
75+ Number . parseInt ( month ) - 1 ,
76+ Number . parseInt ( day ) ,
77+ Number . parseInt ( hours ) ,
78+ Number . parseInt ( minutes ) ,
79+ Number . parseInt ( seconds ) ,
80+ ) ;
81+ return date . toISOString ( ) ;
8282} ;
8383
8484const handleWebSocketLog = ( log : log_message ) => {
85- try {
86- logToClients ( {
87- ...log ,
88- timestamp : parseTimestamp ( log . timestamp ) ,
89- } ) ;
90- } catch ( error ) {
91- console . error (
92- `WebSocket logging failed: ${
93- error instanceof Error ? error . message : error
94- } `
95- ) ;
96- }
85+ try {
86+ logToClients ( {
87+ ...log ,
88+ timestamp : parseTimestamp ( log . timestamp ) ,
89+ } ) ;
90+ } catch ( error ) {
91+ console . error (
92+ `WebSocket logging failed: ${
93+ error instanceof Error ? error . message : error
94+ } ` ,
95+ ) ;
96+ }
9797} ;
9898
9999const handleDatabaseLog = ( log : log_message ) : void => {
100- if ( backupInProgress ) {
101- return ;
102- }
103- try {
104- dbFunctions . addLogEntry ( {
105- ...log ,
106- timestamp : parseTimestamp ( log . timestamp ) ,
107- } ) ;
108- } catch ( error ) {
109- console . error (
110- `Database logging failed: ${
111- error instanceof Error ? error . message : error
112- } `
113- ) ;
114- }
100+ if ( backupInProgress ) {
101+ return ;
102+ }
103+ try {
104+ dbFunctions . addLogEntry ( {
105+ ...log ,
106+ timestamp : parseTimestamp ( log . timestamp ) ,
107+ } ) ;
108+ } catch ( error ) {
109+ console . error (
110+ `Database logging failed: ${
111+ error instanceof Error ? error . message : error
112+ } ` ,
113+ ) ;
114+ }
115115} ;
116116
117117export const logger = createLogger ( {
118- level : process . env . LOG_LEVEL || "debug" ,
119- format : format . combine (
120- format . timestamp ( { format : "DD/MM HH:mm:ss" } ) ,
121- format ( ( info ) => {
122- const stack = new Error ( ) . stack ?. split ( "\n" ) ;
123- let file = "unknown" ;
124- let line = 0 ;
125-
126- if ( stack ) {
127- for ( let i = 2 ; i < stack . length ; i ++ ) {
128- const lineStr = stack [ i ] . trim ( ) ;
129- if (
130- ! lineStr . includes ( "node_modules" ) &&
131- ! lineStr . includes ( path . basename ( import . meta. url ) )
132- ) {
133- const matches = lineStr . match ( / \( ? ( .+ ) : ( \d + ) : ( \d + ) \) ? $ / ) ;
134- if ( matches ) {
135- file = path . basename ( matches [ 1 ] ) ;
136- line = Number . parseInt ( matches [ 2 ] , 10 ) ;
137- break ;
138- }
139- }
140- }
141- }
142- return { ...info , file, line } ;
143- } ) ( ) ,
144- format . printf ( ( info ) => {
145- const { timestamp, level, message, file, line } =
146- info as TransformableInfo & log_message ;
147- let processedLevel = level as LogLevel ;
148- let processedMessage = String ( message ) ;
149-
150- if ( processedMessage . startsWith ( "__task__" ) ) {
151- processedMessage = processedMessage
152- . replace ( / _ _ t a s k _ _ / g, "" )
153- . trimStart ( ) ;
154- processedLevel = "task" ;
155- if ( processedMessage . startsWith ( "__db__" ) ) {
156- processedMessage = processedMessage
157- . replace ( / _ _ d b _ _ / g, "" )
158- . trimStart ( ) ;
159- processedMessage = `${ chalk . magenta ( "DB" ) } ${ processedMessage } ` ;
160- }
161- } else if ( processedMessage . startsWith ( "__UT__" ) ) {
162- processedMessage = processedMessage . replace ( / _ _ U T _ _ / g, "" ) . trimStart ( ) ;
163- processedLevel = "ut" ;
164- }
165-
166- if ( file . endsWith ( "plugin.ts" ) ) {
167- processedMessage = `[ ${ chalk . grey ( file ) } ] ${ processedMessage } ` ;
168- }
169-
170- const paddedLevel = processedLevel . toUpperCase ( ) . padEnd ( 5 ) ;
171- const coloredLevel = ( levelColors [ processedLevel ] || chalk . white ) (
172- paddedLevel
173- ) ;
174- const coloredContext = chalk . cyan ( `${ file } :${ line } ` ) ;
175- const coloredTimestamp = chalk . yellow ( timestamp ) ;
176-
177- const prefix = `${ paddedLevel } [ ${ timestamp } ] - ` ;
178- const combinedContent = `${ processedMessage } - ${ coloredContext } ` ;
179-
180- const formattedMessage = padNewlines
181- ? formatTerminalMessage ( combinedContent , prefix )
182- : combinedContent ;
183-
184- handleDatabaseLog ( {
185- level : processedLevel ,
186- timestamp : timestamp ,
187- message : processedMessage ,
188- file : file ,
189- line : line ,
190- } ) ;
191- handleWebSocketLog ( {
192- level : processedLevel ,
193- timestamp : timestamp ,
194- message : processedMessage ,
195- file : file ,
196- line : line ,
197- } ) ;
198-
199- return `${ coloredLevel } [ ${ coloredTimestamp } ] - ${ formattedMessage } ` ;
200- } )
201- ) ,
202- transports : [ new transports . Console ( ) ] ,
118+ level : process . env . LOG_LEVEL || "debug" ,
119+ format : format . combine (
120+ format . timestamp ( { format : "DD/MM HH:mm:ss" } ) ,
121+ format ( ( info ) => {
122+ const stack = new Error ( ) . stack ?. split ( "\n" ) ;
123+ let file = "unknown" ;
124+ let line = 0 ;
125+
126+ if ( stack ) {
127+ for ( let i = 2 ; i < stack . length ; i ++ ) {
128+ const lineStr = stack [ i ] . trim ( ) ;
129+ if (
130+ ! lineStr . includes ( "node_modules" ) &&
131+ ! lineStr . includes ( path . basename ( import . meta. url ) )
132+ ) {
133+ const matches = lineStr . match ( / \( ? ( .+ ) : ( \d + ) : ( \d + ) \) ? $ / ) ;
134+ if ( matches ) {
135+ file = path . basename ( matches [ 1 ] ) ;
136+ line = Number . parseInt ( matches [ 2 ] , 10 ) ;
137+ break ;
138+ }
139+ }
140+ }
141+ }
142+ return { ...info , file, line } ;
143+ } ) ( ) ,
144+ format . printf ( ( info ) => {
145+ const { timestamp, level, message, file, line } =
146+ info as TransformableInfo & log_message ;
147+ let processedLevel = level as LogLevel ;
148+ let processedMessage = String ( message ) ;
149+
150+ if ( processedMessage . startsWith ( "__task__" ) ) {
151+ processedMessage = processedMessage
152+ . replace ( / _ _ t a s k _ _ / g, "" )
153+ . trimStart ( ) ;
154+ processedLevel = "task" ;
155+ if ( processedMessage . startsWith ( "__db__" ) ) {
156+ processedMessage = processedMessage
157+ . replace ( / _ _ d b _ _ / g, "" )
158+ . trimStart ( ) ;
159+ processedMessage = `${ chalk . magenta ( "DB" ) } ${ processedMessage } ` ;
160+ }
161+ } else if ( processedMessage . startsWith ( "__UT__" ) ) {
162+ processedMessage = processedMessage . replace ( / _ _ U T _ _ / g, "" ) . trimStart ( ) ;
163+ processedLevel = "ut" ;
164+ }
165+
166+ if ( file . endsWith ( "plugin.ts" ) ) {
167+ processedMessage = `[ ${ chalk . grey ( file ) } ] ${ processedMessage } ` ;
168+ }
169+
170+ const paddedLevel = processedLevel . toUpperCase ( ) . padEnd ( 5 ) ;
171+ const coloredLevel = ( levelColors [ processedLevel ] || chalk . white ) (
172+ paddedLevel ,
173+ ) ;
174+ const coloredContext = chalk . cyan ( `${ file } :${ line } ` ) ;
175+ const coloredTimestamp = chalk . yellow ( timestamp ) ;
176+
177+ const prefix = `${ paddedLevel } [ ${ timestamp } ] - ` ;
178+ const combinedContent = `${ processedMessage } - ${ coloredContext } ` ;
179+
180+ const formattedMessage = padNewlines
181+ ? formatTerminalMessage ( combinedContent , prefix )
182+ : combinedContent ;
183+
184+ handleDatabaseLog ( {
185+ level : processedLevel ,
186+ timestamp : timestamp ,
187+ message : processedMessage ,
188+ file : file ,
189+ line : line ,
190+ } ) ;
191+ handleWebSocketLog ( {
192+ level : processedLevel ,
193+ timestamp : timestamp ,
194+ message : processedMessage ,
195+ file : file ,
196+ line : line ,
197+ } ) ;
198+
199+ return `${ coloredLevel } [ ${ coloredTimestamp } ] - ${ formattedMessage } ` ;
200+ } ) ,
201+ ) ,
202+ transports : [ new transports . Console ( ) ] ,
203203} ) ;
0 commit comments