22
33// Modules
44const _ = require ( 'lodash' ) ;
5- const dayjs = require ( 'dayjs' ) ;
65const fs = require ( 'fs' ) ;
76const path = require ( 'path' ) ;
87const serialize = require ( 'winston/lib/winston/common' ) . serialize ;
9- const util = require ( 'util' ) ;
108const winston = require ( 'winston' ) ;
9+ const util = require ( 'util' ) ;
1110
1211// Constants
1312const logLevels = {
@@ -28,12 +27,10 @@ const logColors = {
2827 timestamp : 'magenta' ,
2928 lando : 'cyan' ,
3029 app : 'green' ,
30+ extra : 'dim' ,
3131} ;
3232const userLevels = [ 'warn' , 'error' ] ;
3333
34- // Maxsize
35- let fcw = 0 ;
36-
3734// Rewriters
3835const keySanitizer = sanitizeKey => ( level , msg , meta ) => {
3936 // start with a deep clone of meta so we dont mutate important underlying data
@@ -143,34 +140,44 @@ const keySanitizer = sanitizeKey => (level, msg, meta) => {
143140 * lando.log.warning('Something is up with app %s in directory %s', appName, dir);
144141 */
145142module . exports = class Log extends winston . Logger {
146- constructor ( { logDir, logLevelConsole = 'warn' , logLevel = 'debug' , logName = 'lando' } = { } ) {
143+ constructor ( { logDir, extra , logLevelConsole = 'warn' , logLevel = 'debug' , logName = 'lando' } = { } ) {
147144 // If loglevelconsole is numeric lets map it!
148145 if ( _ . isInteger ( logLevelConsole ) ) logLevelConsole = logLevels [ logLevelConsole ] ;
149146
150147 // The default console transport
151148 const transports = [
152149 new winston . transports . Console ( {
153- timestamp : ( ) => dayjs ( ) . format ( 'HH:mm:ss' ) ,
150+ timestamp : ( ) => Date . now ( ) ,
154151 formatter : options => {
155152 // Get da prefixes
156153 const element = ( logName === 'lando' ) ? 'lando' : logName ;
157154 const elementColor = ( logName === 'lando' ) ? 'lando' : 'app' ;
158- // Set the leftmost column width
159- fcw = _ . max ( [ fcw , _ . size ( element ) ] ) ;
160- // Default output
161- const output = [
162- winston . config . colorize ( elementColor , _ . padEnd ( element . toLowerCase ( ) , fcw ) ) ,
163- winston . config . colorize ( 'timestamp' , options . timestamp ( ) ) ,
164- winston . config . colorize ( options . level , options . level . toUpperCase ( ) ) ,
165- '==>' ,
166- util . format ( options . message ) ,
167- serialize ( options . meta ) ,
168- ] ;
169- // If this is a warning or error and we aren't verbose then omit prefixes
155+
156+ // approximate debug mod timestamp
157+ const curr = Number ( new Date ( ) ) ;
158+ const ms = curr - ( this . lasttime || curr ) ;
159+ this . lasttime = curr ;
160+
161+ // build useful things first
162+ const prefix = winston . config . colorize ( elementColor , element . toLowerCase ( ) ) ;
163+ const level = winston . config . colorize ( options . level , options . level . toUpperCase ( ) ) ;
164+ const msg = util . format ( options . message ) ;
165+ const meta = serialize ( options . meta ) ;
166+ const timestamp = winston . config . colorize ( elementColor , `+${ ms } ms` ) ;
167+
168+ // If this is a warning or error and we aren't verbose then we have more "normal" output
170169 if ( _ . includes ( userLevels , options . level ) && _ . includes ( userLevels , logLevelConsole ) ) {
171- return _ . drop ( output , 2 ) . join ( ' ' ) ;
170+ return [ level , '==>' , msg ] . join ( ' ' ) ;
172171 }
173- return output . join ( ' ' ) ;
172+
173+ // debug output
174+ const output = [ prefix , msg , meta , timestamp ] ;
175+ // if we have extra stuff
176+ if ( typeof extra === 'string' ) output . splice ( 1 , 0 , winston . config . colorize ( 'extra' , extra . toLowerCase ( ) ) ) ;
177+ // if error or warning then try to make it more obvious by splicing in the level
178+ if ( _ . includes ( userLevels , options . level ) ) output . splice ( 1 , 0 , level ) ;
179+ // return
180+ return ` ${ output . join ( ' ' ) } ` ;
174181 } ,
175182 label : logName ,
176183 level : logLevelConsole ,
@@ -204,10 +211,15 @@ module.exports = class Log extends winston.Logger {
204211 // Get the winston logger
205212 super ( { transports : transports , exitOnError : true , colors : logColors } ) ;
206213
214+ // set initial timestamp
215+ this . lasttime = Date . now ( ) ;
207216 // Extend with special lando things
208217 this . sanitizedKeys = [ 'auth' , 'token' , 'password' , 'key' , 'api_key' , 'secret' , 'machine_token' ] ;
209218 // Loop through our sanitizedKeys and add sanitation
210219 _ . forEach ( this . sanitizedKeys , key => this . rewriters . push ( keySanitizer ( key ) ) ) ;
220+
221+ // save the initial config for shiming
222+ this . shim = { logDir, extra, logLevelConsole, logLevel, logName} ;
211223 } ;
212224
213225 // Method to help other things add sanitizations
0 commit comments