diff --git a/core/utils/lifecycle.js b/core/utils/lifecycle.js index 9849a94ae..2befe7b85 100644 --- a/core/utils/lifecycle.js +++ b/core/utils/lifecycle.js @@ -6,7 +6,7 @@ const EventEmitter = require('events') /*:: -import type { Logger } from './logger' +// import type { Logger } from './logger' type State = 'done-stop' | 'will-start' | 'done-start' | 'will-stop' */ @@ -15,10 +15,12 @@ class LifeCycle extends EventEmitter { /*:: currentState: State blockedFor: ?string - log: Logger + // log: Logger + log: any */ - constructor(logger /*: Logger */) { + // constructor(logger /*: Logger */) { + constructor(logger /*: any */) { super() this.currentState = 'done-stop' this.blockedFor = null diff --git a/core/utils/logger.js b/core/utils/logger.js index d3986c91c..78b7d9142 100644 --- a/core/utils/logger.js +++ b/core/utils/logger.js @@ -4,17 +4,17 @@ * @flow weak */ -const fse = require('fs-extra') +// const fse = require('fs-extra') const os = require('os') const path = require('path') -const _ = require('lodash') -const winston = require('winston') -const DailyRotateFile = require('winston-daily-rotate-file') -const { combine, json, splat, timestamp, printf } = winston.format +// const _ = require('lodash') +// const winston = require('winston') +// const DailyRotateFile = require('winston-daily-rotate-file') +// const { combine, json, splat, timestamp, printf } = winston.format -/*:: -export type Logger = winston.Logger -*/ +// /*:: +// export type Logger = winston.Logger +// */ const LOG_DIR = path.join( process.env.COZY_DESKTOP_DIR || os.homedir(), @@ -22,202 +22,217 @@ const LOG_DIR = path.join( ) const LOG_BASENAME = 'logs.txt' -fse.ensureDirSync(LOG_DIR) - -// Remove the `timestamp` field as we use the `time` alias (for backwards -// compatibility with our jq filters. -// -// eslint-disable-next-line no-unused-vars -const dropTimestamp = winston.format(({ timestamp, ...meta }) => ({ ...meta })) - -// Replace winston's string `level` with integers whose values come from -// bunyan for backwards compatibility with out jq filters. -// This has the added advantage of being sligtly lighter. -// -const FATAL_LVL = 60 -const ERROR_LVL = 50 -const WARN_LVL = 40 -const INFO_LVL = 30 -const DEBUG_LVL = 20 -const TRACE_LVL = 10 -const levelToInt = winston.format(({ level, ...meta }) => { - const int = - { - fatal: FATAL_LVL, - error: ERROR_LVL, - warn: WARN_LVL, - info: INFO_LVL, - debug: DEBUG_LVL, - trace: TRACE_LVL - }[level] || 70 - return { level: int, ...meta } -}) - -// Replace `message` with `msg` for backwards compatibility with our jq filters. -const messageToMsg = winston.format(({ message, ...meta }) => ({ - msg: message, - ...meta -})) - -// Allow logging without message. -// -// e.g. log.info({ err, sentry: true }) -// -const objectMsgToMeta = winston.format(({ message, ...meta }) => { - if (typeof message === 'string') { - return { message, ...meta } - } else { - return { message: '', ...message, ...meta } - } -}) - -const hostname = winston.format(info => ({ ...info, hostname: os.hostname() })) - -// Add the process pid to the logs so we can more easily detect when there are -// multiple instances of Desktop running at the same time or if it was -// restarted. -// -const pid = winston.format(info => ({ ...info, pid: process.pid })) - -// Copied from bunyan for backwards compatibility. -// -const getFullErrorStack = err => { - let ret = err.stack || err.toString() - if (err.cause && typeof err.cause === 'function') { - const cerr = err.cause() - if (cerr) { - ret += '\nCaused by: ' + getFullErrorStack(cerr) - } - } - return ret -} -const errSerializer = err => { - if (!err || !err.stack) return err - const obj = { - stack: getFullErrorStack(err), - ..._.pick(err, [ - 'message', - 'name', - 'code', - 'signal', - 'type', - 'reason', - 'address', - 'dest', - 'info', - 'path', - 'port', - 'syscall', - 'code', - 'status', - 'originalErr', - 'errors', - 'doc', - 'incompatibilities', - 'change', - 'data' - ]) - } - return obj +//fse.ensureDirSync(LOG_DIR) + +//// Remove the `timestamp` field as we use the `time` alias (for backwards +//// compatibility with our jq filters. +//// +//// eslint-disable-next-line no-unused-vars +//const dropTimestamp = winston.format(({ timestamp, ...meta }) => ({ ...meta })) + +//// Replace winston's string `level` with integers whose values come from +//// bunyan for backwards compatibility with out jq filters. +//// This has the added advantage of being sligtly lighter. +//// +//const FATAL_LVL = 60 +//const ERROR_LVL = 50 +//const WARN_LVL = 40 +//const INFO_LVL = 30 +//const DEBUG_LVL = 20 +//const TRACE_LVL = 10 +//const levelToInt = winston.format(({ level, ...meta }) => { +// const int = +// { +// fatal: FATAL_LVL, +// error: ERROR_LVL, +// warn: WARN_LVL, +// info: INFO_LVL, +// debug: DEBUG_LVL, +// trace: TRACE_LVL +// }[level] || 70 +// return { level: int, ...meta } +//}) + +//// Replace `message` with `msg` for backwards compatibility with our jq filters. +//const messageToMsg = winston.format(({ message, ...meta }) => ({ +// msg: message, +// ...meta +//})) + +//// Allow logging without message. +//// +//// e.g. log.info({ err, sentry: true }) +//// +//const objectMsgToMeta = winston.format(({ message, ...meta }) => { +// if (typeof message === 'string') { +// return { message, ...meta } +// } else { +// return { message: '', ...message, ...meta } +// } +//}) + +//const hostname = winston.format(info => ({ ...info, hostname: os.hostname() })) + +//// Add the process pid to the logs so we can more easily detect when there are +//// multiple instances of Desktop running at the same time or if it was +//// restarted. +//// +//const pid = winston.format(info => ({ ...info, pid: process.pid })) + +//// Copied from bunyan for backwards compatibility. +//// +//const getFullErrorStack = err => { +// let ret = err.stack || err.toString() +// if (err.cause && typeof err.cause === 'function') { +// const cerr = err.cause() +// if (cerr) { +// ret += '\nCaused by: ' + getFullErrorStack(cerr) +// } +// } +// return ret +//} +//const errSerializer = err => { +// if (!err || !err.stack) return err +// const obj = { +// stack: getFullErrorStack(err), +// ..._.pick(err, [ +// 'message', +// 'name', +// 'code', +// 'signal', +// 'type', +// 'reason', +// 'address', +// 'dest', +// 'info', +// 'path', +// 'port', +// 'syscall', +// 'code', +// 'status', +// 'originalErr', +// 'errors', +// 'doc', +// 'incompatibilities', +// 'change', +// 'data' +// ]) +// } +// return obj +//} +//const error = winston.format(({ err, ...meta }) => ({ +// err: errSerializer(err), +// ...meta +//})) + +//const defaultFormatter = combine( +// objectMsgToMeta(), +// splat(), +// hostname(), +// pid(), +// timestamp({ alias: 'time' }), +// dropTimestamp(), +// error(), +// messageToMsg(), +// levelToInt() +//) + +//const defaultTransport = new DailyRotateFile({ +// level: 'trace', +// dirname: LOG_DIR, +// filename: LOG_BASENAME, +// datePattern: 'YYYY-MM-DD', // XXX: rotate every day +// maxFiles: 7, +// zippedArchive: true, // XXX: gzip archived log files +// format: combine(defaultFormatter, json()) +//}) + +const defaultLogger = { + fatal: () => {}, + error: () => {}, + warn: () => {}, + info: () => {}, + debug: () => {}, + trace: () => {}, + add: () => {}, + child: () => defaultLogger } -const error = winston.format(({ err, ...meta }) => ({ - err: errSerializer(err), - ...meta -})) - -const defaultFormatter = combine( - objectMsgToMeta(), - splat(), - hostname(), - pid(), - timestamp({ alias: 'time' }), - dropTimestamp(), - error(), - messageToMsg(), - levelToInt() -) -const defaultTransport = new DailyRotateFile({ - level: 'trace', - dirname: LOG_DIR, - filename: LOG_BASENAME, - datePattern: 'YYYY-MM-DD', // XXX: rotate every day - maxFiles: 7, - zippedArchive: true, // XXX: gzip archived log files - format: combine(defaultFormatter, json()) -}) - -const defaultLogger = winston.createLogger({ - levels: { - fatal: 0, - error: 1, - warn: 2, - info: 3, - debug: 4, - trace: 5 - }, - level: 'trace', - transports: [defaultTransport] -}) -defaultLogger.on('error', err => { - // eslint-disable-next-line no-console - console.log('failed to log', { err }) -}) - -if (process.env.DEBUG) { - const filename = 'debug.log' - // XXX: Clear log file from previous logs to simplify analysis - fse.outputFileSync(filename, '') - - defaultLogger.add( - new winston.transports.File({ - filename, - format: combine(defaultFormatter, json()) - }) - ) -} - -if (process.env.TESTDEBUG) { - defaultLogger.add( - new winston.transports.Console({ - handleExceptions: true, - format: combine( - splat(), - error(), - printf(({ component, message, ...meta }) => { - let out = component - - if (meta.path) out += ` ${meta.path}` - if (meta._id) out += ` ${meta._id}` - - out += ` ${message}` - - const extra = _.omit(meta, ['level']) - if (Object.keys(extra).length > 0) out += ` ${JSON.stringify(extra)}` - - return out - }) - ) - }) - ) -} - -function logger(options) { - return defaultLogger.child(options) +// const defaultLogger = winston.createLogger({ +// levels: { +// fatal: 0, +// error: 1, +// warn: 2, +// info: 3, +// debug: 4, +// trace: 5 +// }, +// level: 'trace', +// transports: [defaultTransport] +// }) +// defaultLogger.on('error', err => { +// // eslint-disable-next-line no-console +// console.log('failed to log', { err }) +// }) + +// if (process.env.DEBUG) { +// const filename = 'debug.log' +// // XXX: Clear log file from previous logs to simplify analysis +// fse.outputFileSync(filename, '') + +// defaultLogger.add( +// new winston.transports.File({ +// filename, +// format: combine(defaultFormatter, json()) +// }) +// ) +// } + +// if (process.env.TESTDEBUG) { +// defaultLogger.add( +// new winston.transports.Console({ +// handleExceptions: true, +// format: combine( +// splat(), +// error(), +// printf(({ component, message, ...meta }) => { +// let out = component + +// if (meta.path) out += ` ${meta.path}` +// if (meta._id) out += ` ${meta._id}` + +// out += ` ${message}` + +// const extra = _.omit(meta, ['level']) +// if (Object.keys(extra).length > 0) out += ` ${JSON.stringify(extra)}` + +// return out +// }) +// ) +// }) +// ) +// } + +// function logger(options) { +// return defaultLogger.child(options) +// } +// eslint-disable-next-line no-unused-vars +function logger(_ /*: ?Object */) { + return defaultLogger.child() } module.exports = { - FATAL_LVL, - ERROR_LVL, - WARN_LVL, - INFO_LVL, - DEBUG_LVL, - TRACE_LVL, + // FATAL_LVL, + // ERROR_LVL, + // WARN_LVL, + // INFO_LVL, + // DEBUG_LVL, + // TRACE_LVL, LOG_DIR, LOG_BASENAME, - defaultFormatter, + // defaultFormatter, defaultLogger, - dropTimestamp, - logger, - messageToMsg + // dropTimestamp, + logger + // messageToMsg } diff --git a/core/utils/sentry.js b/core/utils/sentry.js index c210dc896..76b3dd225 100644 --- a/core/utils/sentry.js +++ b/core/utils/sentry.js @@ -20,20 +20,20 @@ const { ExtraErrorData: ExtraErrorDataIntegration } = require('@sentry/integrations') const url = require('url') -const _ = require('lodash') -const winston = require('winston') -const { combine, json } = winston.format +// const _ = require('lodash') +// const winston = require('winston') +// const { combine, json } = winston.format const { SESSION_PARTITION_NAME } = require('../../gui/js/network') const { HOURS } = require('./time') const { - FATAL_LVL, - ERROR_LVL, - WARN_LVL, - INFO_LVL, - DEBUG_LVL, - defaultFormatter, - defaultLogger, + // FATAL_LVL, + // ERROR_LVL, + // WARN_LVL, + // INFO_LVL, + // DEBUG_LVL, + // defaultFormatter, + // defaultLogger, logger } = require('./logger') @@ -140,11 +140,11 @@ function setup(clientInfos /*: ClientInfo */) { scope.setTag('instance', instance) scope.setTag('server_name', clientInfos.deviceName) }) - defaultLogger.add( - new SentryTransport({ - format: combine(defaultFormatter, json()) - }) - ) + // defaultLogger.add( + // new SentryTransport({ + // format: combine(defaultFormatter, json()) + // }) + // ) ErrorsAlreadySent = new Map() isSentryConfigured = true log.info('Sentry configured !') @@ -166,56 +166,56 @@ function setup(clientInfos /*: ClientInfo */) { } } -class SentryTransport extends winston.Transport { - constructor(opts) { - super(opts) - } +// class SentryTransport extends winston.Transport { +// constructor(opts) { +// super(opts) +// } - log(info, callback) { - const { component, level, msg, sentry, time, ...meta } = info - const { err } = meta - const cleanMeta = _.omit(meta, ['tags', 'hostname']) - const sentryLevel = - level >= FATAL_LVL - ? 'fatal' - : level >= ERROR_LVL - ? 'error' - : level >= WARN_LVL - ? 'warning' - : level >= DEBUG_LVL - ? 'debug' - : level >= INFO_LVL - ? 'log' - : 'trace' +// log(info, callback) { +// const { component, level, msg, sentry, time, ...meta } = info +// const { err } = meta +// const cleanMeta = _.omit(meta, ['tags', 'hostname']) +// const sentryLevel = +// level >= FATAL_LVL +// ? 'fatal' +// : level >= ERROR_LVL +// ? 'error' +// : level >= WARN_LVL +// ? 'warning' +// : level >= DEBUG_LVL +// ? 'debug' +// : level >= INFO_LVL +// ? 'log' +// : 'trace' - // Send messages explicitly marked for sentry and all TypeError instances - if (sentry || (err && err.name === 'TypeError')) { - const extra = { component, msg, time, ...cleanMeta } +// // Send messages explicitly marked for sentry and all TypeError instances +// if (sentry || (err && err.name === 'TypeError')) { +// const extra = { component, msg, time, ...cleanMeta } - Sentry.withScope(scope => { - scope.setLevel(sentryLevel) - scope.setContext('msgDetails', extra) - scope.setFingerprint([component, err.name, err.message]) +// Sentry.withScope(scope => { +// scope.setLevel(sentryLevel) +// scope.setContext('msgDetails', extra) +// scope.setFingerprint([component, err.name, err.message]) - if (err) { - Sentry.captureException(formatError(err)) - } else { - Sentry.captureMessage(msg) - } - }) - } else { - // keep it as breadcrumb - Sentry.addBreadcrumb({ - message: msg, - category: component, - data: { time, ...cleanMeta }, - level: sentryLevel - }) - } +// if (err) { +// Sentry.captureException(formatError(err)) +// } else { +// Sentry.captureMessage(msg) +// } +// }) +// } else { +// // keep it as breadcrumb +// Sentry.addBreadcrumb({ +// message: msg, +// category: component, +// data: { time, ...cleanMeta }, +// level: sentryLevel +// }) +// } - callback() - } -} +// callback() +// } +// } // TODO: make Flow happy with extended error type function flag(err /*: Object */) { diff --git a/test/property/local_watcher/index.js b/test/property/local_watcher/index.js index 49282d27b..ecefe33cc 100644 --- a/test/property/local_watcher/index.js +++ b/test/property/local_watcher/index.js @@ -10,7 +10,7 @@ const path = require('path') const Promise = require('bluebird') const { id } = require('../../../core/metadata') -const { defaultLogger } = require('../../../core/utils/logger') +// const { defaultLogger } = require('../../../core/utils/logger') const { ContextDir } = require('../../support/helpers/context_dir') const TmpDir = require('../../support/helpers/TmpDir') @@ -32,12 +32,12 @@ describe('Local watcher', function () { let state /*: Object */ = { name: scenario, conflicts: [] } state.dir = new ContextDir(await TmpDir.emptyForTestFile(scenario)) - defaultLogger.streams.length = 0 - defaultLogger.addStream({ - type: 'file', - path: state.dir.root + '.log', - level: 'debug' - }) + // defaultLogger.streams.length = 0 + // defaultLogger.addStream({ + // type: 'file', + // path: state.dir.root + '.log', + // level: 'debug' + // }) await run(state, ops) // Wait that the dust settles diff --git a/test/support/hooks/logging.js b/test/support/hooks/logging.js index e66e5820d..c69eac509 100644 --- a/test/support/hooks/logging.js +++ b/test/support/hooks/logging.js @@ -1,23 +1,24 @@ /* eslint-env mocha */ /* @flow */ -const winston = require('winston') +// const winston = require('winston') -const { defaultLogger, logger } = require('../../../core/utils/logger') +// const { defaultLogger, logger } = require('../../../core/utils/logger') +const { logger } = require('../../../core/utils/logger') const log = logger({ component: 'mocha' }) const errors = [] -defaultLogger.add( - new winston.Transport({ - level: 'error', - log: ({ err, message }, callback) => { - errors.push(err || message) +// defaultLogger.add( +// new winston.Transport({ +// level: 'error', +// log: ({ err, message }, callback) => { +// errors.push(err || message) - callback() - } - }) -) +// callback() +// } +// }) +// ) beforeEach(function () { errors.length = 0