-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [NayNay] Debug Log - install and integrated winston logging library into cli - allowing us to be able to log any and everything to the respective files saved in the ~/path/to/syslogs/entropy-cryptography/entropy-cli.*.log - included example use of new logger in the tui flow for balance * Update logger.ts Co-authored-by: mix irving <[email protected]> * Update logger.ts Co-authored-by: mix irving <[email protected]> * removed debug function and package and updated use of debug logs throughout cli to use new logger; added masking to the payload logged to obfuscate secret information * removed use of env vars and mvoed to options arg in logger constructor * updated changelog --------- Co-authored-by: Nayyir Jutha <[email protected]> Co-authored-by: mixmix <[email protected]>
- Loading branch information
1 parent
a9b4b0b
commit 614f9ec
Showing
24 changed files
with
443 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import envPaths from 'env-paths' | ||
import { join } from 'path' | ||
import * as winston from 'winston' | ||
import { maskPayload } from './masking' | ||
import { EntropyLoggerOptions } from 'src/types' | ||
|
||
/** | ||
* Winston Base Log Levels for NPM | ||
* { | ||
* error: 0, | ||
* warn: 1, | ||
* info: 2, | ||
* http: 3, | ||
* verbose: 4, | ||
* debug: 5, | ||
* silly: 6 | ||
* } | ||
*/ | ||
|
||
export class EntropyLogger { | ||
protected context: string | ||
protected endpoint: string | ||
private winstonLogger: winston.Logger | ||
// TO-DO: update commander with debug, testing, and level options for both programmatic and textual cli | ||
constructor (context: string, endpoint: string, { debug, isTesting, level }: EntropyLoggerOptions = {}) { | ||
this.context = context | ||
this.endpoint = endpoint | ||
|
||
let format = winston.format.combine( | ||
// Add timestamp key: { timestamp: 'YYYY-MM-DD HH:mm:ss.SSS' } | ||
winston.format.timestamp({ | ||
format: 'YYYY-MM-DD HH:mm:ss.SSS', | ||
}), | ||
// If message is instanceof Error, log Error's message property and stack | ||
winston.format.errors({ stack: true }), | ||
// Allows for string interpolation tokens '%s' in message with splat key values | ||
// Ex. { message: 'my message %s', splat: ['test'] } -> { message: 'my message test' } | ||
winston.format.splat(), | ||
// Uses safe-stable-stringify to finalize full object message as string | ||
// (prevents circular references from crashing) | ||
winston.format.json(), | ||
); | ||
|
||
if (isTesting) { | ||
format = winston.format.combine( | ||
format, | ||
winston.format.colorize({ level: true }), | ||
winston.format.printf(info => { | ||
let message = typeof info.message === 'object' ? JSON.stringify(info.message, null, 2) : info.message; | ||
if (info.stack) { | ||
message = `${message}\n${info.stack}`; | ||
} | ||
return `${info.level}: ${message}`; | ||
}), | ||
); | ||
} | ||
const paths = envPaths('entropy-cryptography', { suffix: '' }) | ||
const DEBUG_PATH = join(paths.log, 'entropy-cli.debug.log') | ||
const ERROR_PATH = join(paths.log, 'entropy-cli.error.log') | ||
const INFO_PATH = join(paths.log, 'entropy-cli.info.log') | ||
|
||
this.winstonLogger = winston.createLogger({ | ||
level: level || 'info', | ||
format, | ||
defaultMeta: { service: 'Entropy CLI' }, | ||
transports: [ | ||
new winston.transports.File({ | ||
level: 'error', | ||
filename: ERROR_PATH | ||
}), | ||
new winston.transports.File({ | ||
level: 'info', | ||
filename: INFO_PATH | ||
}), | ||
new winston.transports.File({ | ||
level: 'debug', | ||
filename: DEBUG_PATH, | ||
}), | ||
], | ||
}) | ||
|
||
// If env var is set then stream logs to console as well as a file | ||
if (debug) { | ||
this.winstonLogger.add(new winston.transports.Console({ | ||
format: winston.format.cli() | ||
})) | ||
} | ||
} | ||
|
||
// maps to winston:error | ||
public error (description: string, error: Error): void { | ||
this.writeLogMsg('error', error?.message || error, this.context, description, error.stack); | ||
} | ||
|
||
// maps to winston:info | ||
public log (message: any, context?: string): void { | ||
this.writeLogMsg('info', message, context); | ||
} | ||
|
||
// maps to winston:warn | ||
public warn (message: any, context?: string): void { | ||
this.writeLogMsg('warn', message, context); | ||
} | ||
|
||
// maps to winston:debug | ||
public debug (message: any, context?: string): void { | ||
this.writeLogMsg('debug', message, context); | ||
} | ||
|
||
// maps to winston:verbose | ||
public verbose (message: any, context?: string): void { | ||
this.writeLogMsg('verbose', message, context); | ||
} | ||
|
||
protected writeLogMsg (level: string, message: any, context?: string, description?: string, stack?: string) { | ||
this.winstonLogger.log({ | ||
level, | ||
message: maskPayload(message), | ||
context: context || this.context, | ||
endpoint: this.endpoint, | ||
description, | ||
stack, | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import cloneDeep from 'lodash.clonedeep' | ||
|
||
const DEFAULT_MASKED_FIELDS = [ | ||
'seed', | ||
'secretKey', | ||
'addressRaw', | ||
]; | ||
|
||
export function maskPayload (payload: any): any { | ||
const clonedPayload = cloneDeep(payload); | ||
const maskedPayload = {} | ||
|
||
if (!clonedPayload) { | ||
return clonedPayload; | ||
} | ||
|
||
// maskJSONFields doesn't handle nested objects very well so we'll | ||
// need to recursively walk to object and mask them one by one | ||
for (const [property, value] of Object.entries(clonedPayload)) { | ||
console.log(property, typeof value); | ||
|
||
if (value && typeof value === 'object') { | ||
if (Object.keys(clonedPayload[property]).filter(key => isNaN(parseInt(key))).length === 0) { | ||
const reconstructedUintArr: number[] = Object.values(clonedPayload[property]) | ||
maskedPayload[property] = "base64:" + Buffer.from(reconstructedUintArr).toString("base64"); | ||
} else { | ||
maskedPayload[property] = maskPayload(value); | ||
} | ||
} else if (value && typeof value === 'string' && DEFAULT_MASKED_FIELDS.includes(property)) { | ||
maskedPayload[property] = "*".repeat(clonedPayload[property].length) | ||
} else { | ||
maskedPayload[property] = value | ||
} | ||
} | ||
|
||
return maskedPayload; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.