66 * MIT Licensed
77 */
88
9- import { Request , Response , NextFunction } from 'express' ;
109import chalk from 'chalk' ;
10+ import fs from 'fs' ;
1111
1212type colorTypes =
1313 | 'black'
@@ -33,6 +33,7 @@ type loggerParams = {
3333 message : string ;
3434 infoColor ?: colorTypes ;
3535 messageColor ?: colorTypes ;
36+ logs ?: string ;
3637} ;
3738
3839type statusCodeType = {
@@ -56,6 +57,7 @@ type BorgenParams = {
5657 statusColor ?: colorTypes ;
5758 resTimeColor ?: colorTypes ;
5859 statusCodesCl ?: statusCodeType ;
60+ logs ?: string ;
5961} ;
6062
6163export class Logger {
@@ -66,15 +68,21 @@ export class Logger {
6668 * @param message The message you want logged `This is an informational message`
6769 * @example
6870 * ```js
69- * Logger.error({message:'This is an info message',infoColor:'blue',messageColor:'cyan'});
71+ * Logger.error({message:'This is an info message',infoColor:'blue',messageColor:'cyan',logs:'../logs/info.log' });
7072 *
7173 * [2/27/2023, 3:57:45 PM] [INFO] Server is listening on port:3001
7274 * ```
7375 */
74- public static info = ( { message, infoColor, messageColor } : loggerParams ) => {
76+ public static info = ( { message, infoColor, messageColor, logs } : loggerParams ) => {
7577 const infoCl = infoColor || 'blue' ;
7678 const messageCl = messageColor || 'blueBright' ;
7779
80+ if ( logs ) {
81+ const output = fs . createWriteStream ( logs , { encoding : 'utf8' , flags : 'a' } ) ;
82+
83+ output . write ( `[${ new Date ( ) . toLocaleString ( ) } ] [INFO] ${ message || 'Default info message' } \n` ) ;
84+ }
85+
7886 console . log ( chalk [ infoCl ] ( `[${ new Date ( ) . toLocaleString ( ) } ] [INFO]` ) , chalk [ messageCl ] ( message || 'Default info message' ) ) ;
7987 } ;
8088
@@ -85,16 +93,22 @@ export class Logger {
8593 * @param message The message you want logged `This is a warning`
8694 * @example
8795 * ```js
88- * Logger.warn({message:'This is an warning',infoColor:'yellow',messageColor:'yellowBright'});
96+ * Logger.warn({message:'This is an warning',infoColor:'yellow',messageColor:'yellowBright',logs:'../logs/warns.log' });
8997 *
9098 * [2/27/2023, 3:57:48 PM] [ERROR] This is a warning
9199 * ```
92100 */
93- public static warn = ( { message, infoColor, messageColor } : loggerParams ) => {
101+ public static warn = ( { message, infoColor, messageColor, logs } : loggerParams ) => {
94102 const infoCl = infoColor || 'yellow' ;
95103 const messageCl = messageColor || 'yellowBright' ;
96104
97- console . log ( chalk [ infoCl ] . bold ( `[${ new Date ( ) . toLocaleString ( ) } ] [WARN]` ) , chalk [ messageCl ] ( message ) ) ;
105+ if ( logs ) {
106+ const output = fs . createWriteStream ( logs , { encoding : 'utf8' , flags : 'a' } ) ;
107+
108+ output . write ( `[${ new Date ( ) . toLocaleString ( ) } ] [INFO] ${ message || 'Default warn message' } \n` ) ;
109+ }
110+
111+ console . log ( chalk [ infoCl ] . bold ( `[${ new Date ( ) . toLocaleString ( ) } ] [WARN]` ) , chalk [ messageCl ] ( message || 'Default warn message' ) ) ;
98112 } ;
99113
100114 /**
@@ -104,16 +118,22 @@ export class Logger {
104118 * @param message The message you want logged `This is an error message`
105119 * @example
106120 * ```js
107- * Logger.error({message:'This is an error message',infoColor:'red',messageColor:'redBright'});
121+ * Logger.error({message:'This is an error message',infoColor:'red',messageColor:'redBright',logs:'../logs/error.log' });
108122 *
109123 * [2/27/2023, 3:57:48 PM] [ERROR] This is route does not exist
110124 * ```
111125 */
112- public static error = ( { message, infoColor, messageColor } : loggerParams ) => {
126+ public static error = ( { message, infoColor, messageColor, logs } : loggerParams ) => {
113127 const infoCl = infoColor || 'red' ;
114128 const messageCl = messageColor || 'redBright' ;
115129
116- console . log ( chalk [ infoCl ] . bold ( `[${ new Date ( ) . toLocaleString ( ) } ] [ERROR]` ) , chalk [ messageCl ] ( message ) ) ;
130+ if ( logs ) {
131+ const output = fs . createWriteStream ( logs , { encoding : 'utf8' , flags : 'a' } ) ;
132+
133+ output . write ( `[${ new Date ( ) . toLocaleString ( ) } ] [INFO] ${ message || 'Default error message' } \n` ) ;
134+ }
135+
136+ console . log ( chalk [ infoCl ] . bold ( `[${ new Date ( ) . toLocaleString ( ) } ] [ERROR]` ) , chalk [ messageCl ] ( message || 'Default error message' ) ) ;
117137 } ;
118138}
119139
@@ -137,15 +157,16 @@ export class Logger {
137157 * routeColor:'gray',
138158 * statusColor:'cyan',
139159 * resTimeColor:'gray',
140- * statusCodesCl:{serverErr:'red', clientErr:'yellow', redirects:'cyan',success:'greenBright'
141- * }})
160+ * statusCodesCl:{serverErr:'red', clientErr:'yellow', redirects:'cyan',success:'greenBright'},
161+ * logs:'../logs/borgen.log',
162+ * })
142163 * ```
143164 * @example
144165 * ```js
145166 * [POST] Request path: /api/v1/users/new - Status:200 in 5 ms
146167 * ```
147168 */
148- export const Borgen = ( { methodColor, routeColor, statusColor, resTimeColor, statusCodesCl } : BorgenParams ) => {
169+ export const Borgen = ( { methodColor, routeColor, statusColor, resTimeColor, statusCodesCl, logs } : BorgenParams ) => {
149170 const { serverErr, clientErr, redirects, success } = {
150171 serverErr : statusCodesCl ?. serverErr || 'red' ,
151172 clientErr : statusCodesCl ?. clientErr || 'yellow' ,
@@ -164,10 +185,18 @@ export const Borgen = ({ methodColor, routeColor, statusColor, resTimeColor, sta
164185 const statusCl = statusColor || 'cyan' ;
165186 const resTimeCl = resTimeColor || 'gray' ;
166187
167- return ( req : Request , res : Response , next : NextFunction ) => {
188+ //@ts -ignore
189+ return ( req , res , next ) => {
168190 const start = Date . now ( ) ;
169- const reqColor = res . on ( 'finish' , ( ) => {
191+ res . on ( 'finish' , ( ) => {
170192 const elapsed = Date . now ( ) - start ;
193+
194+ if ( logs ) {
195+ const output = fs . createWriteStream ( logs , { encoding : 'utf8' , flags : 'a' } ) ;
196+
197+ output . write ( `[${ new Date ( ) . toLocaleString ( ) } ] ${ req . method } Request path: ${ req . path } - Status:${ res . statusCode } in ${ elapsed } ms\n` ) ;
198+ }
199+
171200 console . log (
172201 chalk [ req . method === 'GET' ? GET : req . method === 'POST' ? POST : req . method === 'PUT' ? PUT : req . method === 'PATCH' ? PATCH : req . method === 'DELETE' ? DELETE : 'greenBright' ] (
173202 `[${ req . method } ] Request`
@@ -186,7 +215,6 @@ export const Borgen = ({ methodColor, routeColor, statusColor, resTimeColor, sta
186215 chalk [ resTimeCl ] . bold ( `in ${ elapsed } ms` )
187216 ) ;
188217 } ) ;
189-
190218 next ( ) ;
191219 } ;
192220} ;
0 commit comments