Skip to content

Commit

Permalink
feat: Replace bunyan logger with winston (#2343)
Browse files Browse the repository at this point in the history
The `bunyan` project is unmaintained and relies on another
unmaintained project, `dtrace-provider`, which does not compile with
recent Windows build toolchains which are in turned required to
compile recent versions of Electron.

Therefore we replace `bunyan` with the `winston` logger which is a
modern logger still maintained.
We made some adaptations for backwards compatibility with our jq
filters and our usage of the logger.
  • Loading branch information
taratatach authored Sep 18, 2024
2 parents aceb1b2 + d12c934 commit b5531c0
Show file tree
Hide file tree
Showing 69 changed files with 1,242 additions and 1,015 deletions.
129 changes: 76 additions & 53 deletions core/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const { Remote } = require('./remote')
const { Sync } = require('./sync')
const SyncState = require('./syncstate')
const Registration = require('./remote/registration')
const logger = require('./utils/logger')
const { LOG_FILE, LOG_FILENAME } = logger
const { logger, LOG_BASENAME } = require('./utils/logger')
const { sendToTrash } = require('./utils/fs')
const notes = require('./utils/notes')
const web = require('./utils/web')
Expand Down Expand Up @@ -79,7 +78,7 @@ class App {

// basePath is the directory where the config and pouch are saved
constructor(basePath /*: string */) {
log.info(this.clientInfo(), 'constructor')
log.info('constructor', this.clientInfo())
this.lang = 'fr'
if (basePath == null) {
basePath = os.homedir()
Expand Down Expand Up @@ -177,20 +176,19 @@ class App {
} catch (err) {
let parsed /*: Object */ = this.parseCozyUrl(cozyUrl)
if (err === 'Bad credentials') {
log.warn(
{ err },
'The Cozy passphrase used for registration is incorrect'
)
log.warn('The Cozy passphrase used for registration is incorrect', {
err
})
} else if (err.code === 'ENOTFOUND') {
log.warn(
{ err },
`The DNS resolution for ${parsed.hostname} failed while registering the device.`
`The DNS resolution for ${parsed.hostname} failed while registering the device.`,
{ err }
)
} else {
log.error(
{ err, sentry: true },
'An error occured while registering the device.'
)
log.error('An error occured while registering the device.', {
err,
sentry: true
})
if (parsed.protocol === 'http:') {
log.warn('Did you try with an httpS URL?')
}
Expand All @@ -214,10 +212,10 @@ class App {
log.info('Current device properly removed from remote cozy.')
return null
} catch (err) {
log.error(
{ err, sentry: true },
'An error occured while unregistering the device.'
)
log.error('An error occured while unregistering the device.', {
err,
sentry: true
})
return err
}
}
Expand All @@ -226,7 +224,7 @@ class App {
log.info('Removing config...')
await this.pouch.db.destroy()
for (const name of await fse.readdir(this.basePath)) {
if (name.startsWith(LOG_FILENAME)) continue
if (name.startsWith(LOG_BASENAME)) continue
await fse.remove(path.join(this.basePath, name))
}
log.info('Config removed')
Expand Down Expand Up @@ -266,34 +264,57 @@ class App {
})
}

async listLogFiles() {
let logFiles = []
for (const name of await fse.readdir(this.basePath)) {
if (name.startsWith(LOG_BASENAME)) {
logFiles.push(path.join(this.basePath, name))
}
}
return logFiles
}

// Send an issue by mail to the support
async sendMailToSupport(content /*: string */) {
const incidentID = uuid()
const zipper = createGzip({
// TODO tweak this values, low resources for now.
memLevel: 7,
level: 3
})
const logs = fse.createReadStream(LOG_FILE)

let pouchdbTree /*: ?Metadata[] */
try {
pouchdbTree = await this.pouch.localTree()
} catch (err) {
log.error({ err, sentry: true }, 'FAILED TO FETCH LOCAL TREE')
const sendLogs = async () => {
const logFiles = await this.listLogFiles()

if (logFiles.length === 0) {
return
} else {
const zipper = createGzip({
// TODO tweak this values, low resources for now.
memLevel: 7,
level: 3
})
const logs = fse.createReadStream(logFiles[logFiles.length - 1])

return this.uploadFileToSupport(
incidentID,
'logs.gz',
logs.pipe(zipper)
)
}
}

const logsSent = Promise.all([
this.uploadFileToSupport(incidentID, 'logs.gz', logs.pipe(zipper)),
pouchdbTree
? this.uploadFileToSupport(
incidentID,
'pouchdtree.json',
JSON.stringify(pouchdbTree)
)
: Promise.resolve()
]).catch(err => {
log.error({ err, sentry: true }, 'FAILED TO SEND LOGS')
const sendPouchDBTree = async () => {
const pouchdbTree = await this.pouch.localTree()

if (!pouchdbTree) {
return
} else {
return this.uploadFileToSupport(
incidentID,
'pouchdtree.json',
JSON.stringify(pouchdbTree)
)
}
}

const logsSent = Promise.all([sendLogs(), sendPouchDBTree()]).catch(err => {
log.error('FAILED TO SEND LOGS', { err, sentry: true })
})

content =
Expand Down Expand Up @@ -351,7 +372,7 @@ class App {

async setup() {
const clientInfo = this.clientInfo()
log.info(clientInfo, 'user config')
log.info('user config', clientInfo)

if (!this.config.isValid()) {
throw new config.InvalidConfigError()
Expand All @@ -362,10 +383,11 @@ class App {
try {
this.config.version = clientInfo.appVersion
} catch (err) {
log.error(
{ err, clientInfo, sentry: true },
'could not update config version after app update'
)
log.error('could not update config version after app update', {
err,
clientInfo,
sentry: true
})
wasUpdated = false
}

Expand All @@ -380,10 +402,10 @@ class App {
this.config.setFlag(config.WINDOWS_DATE_MIGRATION_FLAG, true)
}
} catch (err) {
log.error(
{ err, sentry: true },
`could not set ${config.WINDOWS_DATE_MIGRATION_FLAG} flag`
)
log.error(`could not set ${config.WINDOWS_DATE_MIGRATION_FLAG} flag`, {
err,
sentry: true
})
}
}

Expand All @@ -396,10 +418,11 @@ class App {
try {
this.remote.update()
} catch (err) {
log.error(
{ err, config: this.config, sentry: true },
'could not update OAuth client after app update'
)
log.error('could not update OAuth client after app update', {
err,
config: this.config,
sentry: true
})
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const _ = require('lodash')
const path = require('path')

const { hideOnWindows } = require('./utils/fs')
const logger = require('./utils/logger')
const { logger } = require('./utils/logger')

const log = logger({
component: 'Config'
Expand Down Expand Up @@ -288,7 +288,7 @@ function loadOrDeleteFile(configPath /*: string */) /*: FileConfig */ {
return JSON.parse(content)
} catch (e) {
if (e instanceof SyntaxError) {
log.error({ err: e }, `Could not read config file at ${configPath}`)
log.error(`Could not read config file at ${configPath}`, { err: e })
fse.unlinkSync(configPath)
return {}
} else {
Expand Down Expand Up @@ -333,7 +333,7 @@ function validateWatcherType(watcherType /*: ?string */) /*: ?WatcherType */ {
if (watcherType === 'channel' || watcherType === 'chokidar') {
return watcherType
} else {
if (watcherType) log.warn({ watcherType }, 'Invalid watcher type')
if (watcherType) log.warn('Invalid watcher type', { watcherType })
return null
}
}
Expand Down
11 changes: 5 additions & 6 deletions core/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const { basename, dirname, resolve } = require('path')
const { matcher } = require('micromatch')
const fs = require('fs')

const logger = require('./utils/logger')
const { logger } = require('./utils/logger')

const log = logger({
component: 'Ignore'
Expand All @@ -73,12 +73,11 @@ function loadSync(rulesFilePath /*: string */) /*: Ignore */ {
ignored = readLinesSync(rulesFilePath)
} catch (err) {
if (err.code === 'ENOENT') {
log.info(
{ rulesFilePath },
'Skip loading of non-existent ignore rules file'
)
log.info('Skip loading of non-existent ignore rules file', {
rulesFilePath
})
} else {
log.warn({ rulesFilePath, err }, 'Failed loading ignore rules file')
log.warn('Failed loading ignore rules file', { rulesFilePath, err })
}
ignored = []
}
Expand Down
6 changes: 3 additions & 3 deletions core/local/channel_watcher/add_checksum.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const _ = require('lodash')
const path = require('path')

const logger = require('../../utils/logger')
const { logger } = require('../../utils/logger')

const STEP_NAME = 'addChecksum'

Expand Down Expand Up @@ -64,14 +64,14 @@ function loop(
if (isFileWithContent(event) && !event.md5sum) {
const absPath = path.join(syncPath, event.path)
event.md5sum = await opts.checksumer.push(absPath)
log.debug({ path: event.path, event }, 'computed checksum')
log.debug('computed checksum', { path: event.path, event })
}
} catch (err) {
// Even if the file is no longer at the expected path, we want to
// keep the event. Maybe it was one if its parents directory that was
// moved, and then we can refine the event later (in incompleteFixer).
_.set(event, ['incomplete', STEP_NAME], err.message)
log.debug({ err, event }, 'Cannot compute checksum')
log.debug('Cannot compute checksum', { err, event })
}
}
return events
Expand Down
8 changes: 4 additions & 4 deletions core/local/channel_watcher/add_infos.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const _ = require('lodash')
const path = require('path')

const { kind } = require('../../metadata')
const logger = require('../../utils/logger')
const { logger } = require('../../utils/logger')
const stater = require('../stater')

const STEP_NAME = 'addInfos'
Expand Down Expand Up @@ -50,7 +50,7 @@ function loop(
const batch = []
for (const event of events) {
if (event.kind === 'symlink') {
log.warn({ event }, 'Symlinks are not supported')
log.warn('Symlinks are not supported', { event })
// TODO display an error in the UI
continue
}
Expand All @@ -62,7 +62,7 @@ function loop(
try {
if (event.action !== 'initial-scan-done') {
if (needsStats(event)) {
log.debug({ path: event.path, action: event.action }, 'stat')
log.debug('stat', { path: event.path, action: event.action })
event.stats = await stater.stat(path.join(syncPath, event.path))
}

Expand All @@ -89,7 +89,7 @@ function loop(
}
}
} catch (err) {
log.debug({ err, event }, 'Cannot get infos')
log.debug('Cannot get infos', { err, event })
_.set(event, ['incomplete', STEP_NAME], err.message)
}
batch.push(event)
Expand Down
14 changes: 7 additions & 7 deletions core/local/channel_watcher/await_write_finish.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const _ = require('lodash')

const Channel = require('./channel')
const logger = require('../../utils/logger')
const { logger } = require('../../utils/logger')

const STEP_NAME = 'awaitWriteFinish'

Expand Down Expand Up @@ -121,8 +121,8 @@ function aggregateEvents(oldEvent, recentEvent) {
if (oldEvent.action === 'created') {
// It's just a temporary file that we can ignore
log.debug(
{ createdEvent: oldEvent, deletedEvent: recentEvent },
`Ignore ${oldEvent.kind} ${oldEvent.action} then ${recentEvent.action}`
`Ignore ${oldEvent.kind} ${oldEvent.action} then ${recentEvent.action}`,
{ createdEvent: oldEvent, deletedEvent: recentEvent }
)

return
Expand Down Expand Up @@ -217,10 +217,10 @@ function debounce(waiting /*: WaitingItem[] */, events /*: ChannelEvent[] */) {
if (event.action === 'deleted') {
if (e.action === 'created') {
// It's just a temporary file that we can ignore
log.debug(
{ createdEvent: e, deletedEvent: event },
`Ignore ${e.kind} ${e.action} then ${event.action}`
)
log.debug(`Ignore ${e.kind} ${e.action} then ${event.action}`, {
createdEvent: e,
deletedEvent: event
})
events.splice(i, 1)
i--
} else if (e.action === 'renamed') {
Expand Down
Loading

0 comments on commit b5531c0

Please sign in to comment.