@@ -3,6 +3,10 @@ import path from "path";
33import chalk , { ChalkInstance } from "chalk" ;
44import { dbFunctions } from "../database/repository" ;
55import wrapAnsi from "wrap-ansi" ;
6+ import { logToClients } from "~/routes/live-logs" ;
7+ import { logStreamData } from "~/typings/websocket" ;
8+
9+ const ansiRegex = / \x1B \[ [ 0 - ? 9 ; ] * [ m G ] / g;
610
711// Change to false here if dont want the spacing on a wrapped line
812const padNewlines : boolean = true ;
@@ -80,31 +84,40 @@ export const logger = createLogger({
8084 message = `[ ${ chalk . greenBright ( "Plugin" ) } ] ${ message } ` ;
8185 }
8286
87+ const logStreamData : logStreamData = {
88+ timestamp : timestamp as string ,
89+ level : level as string ,
90+ message : ( message as string ) . replace ( ansiRegex , "" ) ,
91+ file : file as string ,
92+ line : line as number ,
93+ } ;
94+
95+ logToClients ( logStreamData ) ;
96+
8397 const paddedLevel = level . toUpperCase ( ) . padEnd ( 5 ) ;
8498 const coloredLevel = ( levelColors [ level ] || chalk . white ) ( paddedLevel ) ;
8599 const coloredContext = chalk . cyan ( `${ file as string } :${ line as number } ` ) ;
86100 const coloredTimestamp = chalk . yellow ( timestamp ) ;
87101
88102 if ( process . env . NODE_ENV !== "dev" ) {
89103 return `${ coloredLevel } [ ${ coloredTimestamp } ] - ${ chalk . gray (
90- message ,
104+ message
91105 ) } - [ ${ coloredContext } ]`;
92106 }
93107
94108 const prefix = `${ paddedLevel } [ ${ timestamp } ] - ` ;
95109 const prefixLength = prefix . length ;
96110 const formattedMessage = formatTerminalMessage (
97111 message as string ,
98- prefixLength ,
112+ prefixLength
99113 ) ;
100- const ansiRegex = / \x1B \[ [ 0 - ? 9 ; ] * [ m G ] / g;
101114
102115 try {
103116 dbFunctions . addLogEntry (
104117 ( level as string ) . replace ( ansiRegex , "" ) ,
105118 ( message as string ) . replace ( ansiRegex , "" ) ,
106119 ( file as string ) . replace ( ansiRegex , "" ) ,
107- line as number ,
120+ line as number
108121 ) ;
109122 } catch ( error ) {
110123 // Use console.error to avoid recursive logging
@@ -113,7 +126,7 @@ export const logger = createLogger({
113126 }
114127
115128 return `${ coloredLevel } [ ${ coloredTimestamp } ] - ${ formattedMessage } - [ ${ coloredContext } ]` ;
116- } ) ,
129+ } )
117130 ) ,
118131 transports : [ new transports . Console ( ) ] ,
119132} ) ;
0 commit comments