import * as Path from 'path' ; import * as winston from 'winston' ; import { ensureDir } from 'fs-extra' ; import { app } from 'electron' ; import { LogLevel } from './levels' ; import DailyRotateFile from 'winston-daily-rotate-file' ; const { combine , timestamp , printf } = winston . format ; let logDirectoryPath = null ; export function getLogDirectoryPath ( ) { if ( ! logDirectoryPath ) { const userData = app . getPath ( 'userData' ) ; logDirectoryPath = Path . join ( userData , 'logs' ) ; } return logDirectoryPath ; } param path function initializeWinston ( path ) { const fileLogger = new DailyRotateFile ( { dirname : path , filename : 'lode-%DATE%.log' , datePattern : 'YYYY-MM-DD' , level : 'info' , maxFiles : '14d' , format : combine ( timestamp ( ) , printf ( ( { level , message , timestamp } ) => { return `${ timestamp } - ${ level } ${ message }` ; } ) ) } ) ; const consoleLogger = new winston . transports . Console ( { level : __DEV__ ? 'debug' : 'error' } ) ; winston . configure ( { transports : [ consoleLogger , fileLogger ] } ) ; return winston . log ; } let loggerPromise = null ; returns function getLogger ( ) { if ( loggerPromise ) { return loggerPromise ; } loggerPromise = new Promise < winston . LogMethod > ( ( resolve , reject ) => { const logDirectory = getLogDirectoryPath ( ) ; ensureDir ( logDirectory ) . then ( ( ) => { try { const logger = initializeWinston ( logDirectory ) ; resolve ( logger ) ; } catch ( err ) { reject ( err ) ; } } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; return loggerPromise ; } /**
 * Write the given log entry to all configured transports,
 * see initializeWinston in logger.ts for more details about
 * what transports we set up.
 *
 * Returns a promise that will never yield an error and which
 * resolves when the log entry has been written to all transports
 * or if the entry could not be written due to an error.
 */ export async function log ( level , message ) { if ( ! __LOGGER__ ) { console . log ( message ) ; return ; } try { const logger = await getLogger ( ) ; await new Promise < void > ( ( resolve , reject ) => { logger ( level , message , error => { if ( error ) { reject ( error ) ; } else { resolve ( ) ; } } ) ; } ) ; } catch ( error ) { } }