feat(core): option to specify the log file

pull/224/head
Romain 3 years ago
parent 0f42dc8f85
commit 3ced05ad1f

@ -1,6 +1,5 @@
const debug = require('debug')
const fs = require('fs')
const path = require('path')
const util = require('util')
const tty = require('tty')
@ -24,7 +23,7 @@ const tty = require('tty')
If --log is not specified, but the output doesn't support ANSI (e.g. non-TTY terminal, or file redirection)
then the mode is automatically switched to "--log info"
*/
exports.init = (logLevel, outputFolder) => {
exports.init = (logLevel, logFile) => {
// if the output doesn't support ANSI codes (e.g. pipe, redirect to file)
// then switch to full-text mode, because Listr's output won't make much sense
if (logLevel === 'default' && !tty.isatty(process.stdout.fd)) {
@ -33,7 +32,7 @@ exports.init = (logLevel, outputFolder) => {
// Configure the loggers
if (logLevel === 'default') {
configureDefaultMode(outputFolder)
configureDefaultMode(logFile)
} else {
configureDebugMode(logLevel)
}
@ -55,9 +54,8 @@ function overrideDebugFormat () {
If --log is not specified, we won't show any detailed log on stdout
Instead we send all errors to a file for troubleshooting
*/
function configureDefaultMode (outputFolder) {
const logfile = path.join(outputFolder, 'thumbsup.log')
const stream = fs.createWriteStream(logfile, { flags: 'a' })
function configureDefaultMode (logFile) {
const stream = fs.createWriteStream(logFile, { flags: 'a' })
overrideDebugFormat()
debug.enable('thumbsup:error,thumbsup:warn')
debug.useColors = () => false

@ -3,7 +3,6 @@ const boxen = require('boxen')
const DOCS_URL = chalk.green('https://thumbsup.github.io/docs')
const ISSUES_URL = chalk.green('https://github.com/thumbsup/thumbsup/issues')
const LOG_FILE = chalk.green('thumbsup.log')
function box (str) {
const lines = str.split('\n').map(s => ` ${s} `).join('\n')
@ -51,13 +50,13 @@ This welcome message will not be shown again for this gallery.
Enjoy!
`)
exports.SORRY = () => box(`
exports.SORRY = (logFile) => box(`
Something went wrong!
An unexpected error occurred and the gallery didn't build successfully.
This is most likely an edge-case that hasn't been tested before.
Please check ${LOG_FILE} in the output folder.
Please check the logs at ${chalk.green(logFile)}.
To help improve thumbsup and hopefully resolve your problem,
you can raise an issue at ${ISSUES_URL}.
`)

@ -331,6 +331,12 @@ const OPTIONS = {
normalize: true
},
'log-file': {
group: 'Misc options:',
description: 'Path to the log file',
normalize: true
},
'log': {
group: 'Misc options:',
description: 'Print a detailed text log',
@ -422,10 +428,16 @@ exports.get = (args) => {
opts.databaseFile = path.join(opts.output, 'thumbsup.db')
}
// Default log file
if (!opts.logFile) {
opts.logFile = changeExtension(opts.databaseFile, '.log')
}
// Better to work with absolute paths
opts.input = path.resolve(opts.input)
opts.output = path.resolve(opts.output)
opts.databaseFile = path.resolve(opts.databaseFile)
opts.logFile = path.resolve(opts.logFile)
// By default, use relative links to the input folder
if (opts.downloadLinkPrefix) opts.linkPrefix = opts.downloadLinkPrefix
@ -469,3 +481,9 @@ function commaSeparated (value) {
if (value.indexOf(',') === -1) return value
return value.split(',')
}
function changeExtension (file, ext) {
const originalExt = path.extname(file)
const filename = path.basename(file, originalExt)
return path.join(path.dirname(file), filename + ext)
}

@ -15,7 +15,7 @@ const opts = options.get(args)
// Only require the index after logging options have been set
fs.mkdirpSync(opts.output)
require('./log').init(opts.log, opts.output)
require('./log').init(opts.log, opts.logFile)
const index = require('../src/index')
// If this is the first run, display a welcome message
@ -72,7 +72,7 @@ function handleError (err) {
delete err.context
require('debug')('thumbsup:error')(err)
console.error('\nUnexpected error', err.message)
console.error(`\n${messages.SORRY()}\n`)
console.error(`\n${messages.SORRY(opts.logFile)}\n`)
exit(1)
}

@ -96,7 +96,7 @@ describe('options', function () {
const opts = options.get(BASE_ARGS)
should(opts.databaseFile).eql(path.resolve('website/thumbsup.db'))
})
it('can be overridden with a relative url', () => {
it('can overridde with a relative url', () => {
const args = BASE_ARGS.concat(['--database-file', 'album.db'])
const opts = options.get(args)
should(opts.databaseFile).eql(path.join(process.cwd(), 'album.db'))
@ -107,6 +107,22 @@ describe('options', function () {
should(opts.databaseFile).eql('/media/album.db')
})
})
describe('log file path', () => {
it('defaults to the output folder', () => {
const opts = options.get(BASE_ARGS)
should(opts.logFile).eql(path.resolve('website/thumbsup.log'))
})
it('is written next to the database file if specified', () => {
const args = BASE_ARGS.concat(['--database-file', 'album.db'])
const opts = options.get(args)
should(opts.logFile).eql(path.join(process.cwd(), 'album.log'))
})
it('can be specified explicitely', () => {
const args = BASE_ARGS.concat(['--log-file', 'custom.log'])
const opts = options.get(args)
should(opts.logFile).eql(path.join(process.cwd(), 'custom.log'))
})
})
})
describe('deprecated', () => {
it('--original-photos false', () => {

Loading…
Cancel
Save