Skip to content

Commit

Permalink
Logging with Verbose option was refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-karatsiuba committed Feb 27, 2023
1 parent 2cf2029 commit c3919c6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
16 changes: 8 additions & 8 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
const logLevels = ['info', 'log', 'warn', 'error']
type LogLevel = (typeof logLevels)[number]
export const logLevels = ['info', 'log', 'warn', 'error', 'quiet']
export type LogLevel = (typeof logLevels)[number]

const isVerbose = (level: LogLevel) => {
const verboseLevel = (process as any).verbose
let verboseLevel = 'error'

const isVerbose = (level: LogLevel) => {
if (!verboseLevel) {
return false
}

if (!logLevels.includes(verboseLevel)) {
return true
}

const verboseLevelIndex = logLevels.indexOf(verboseLevel)
const logLevelIndex = logLevels.indexOf(level)
if (logLevelIndex >= verboseLevelIndex) {
Expand All @@ -31,6 +27,10 @@ const getTs = () => {
return new Date().toISOString()
}

export const setVerboseLevel = (level: LogLevel | true) => {
verboseLevel = level === true ? 'info' : level
}

export const info = (...args: any[]) => {
doLog('info', args)
}
Expand Down
1 change: 0 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const argv: any = yargs(hideBin(process.argv))
/* ***********************************/

import fs from 'fs'
;(process as any).verbose = argv.verbose

if (argv.osTmpDir) {
const osTmpDir = argv.osTmpDir as string
Expand Down
2 changes: 1 addition & 1 deletion src/mwoffliner.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function execute(argv: any) {

let { articleList, articleListToIgnore } = argv

;(process as any).verbose = verbose
if (verbose) logger.setVerboseLevel(verbose)

logger.log(`Starting mwoffliner v${packageJSON.version}...`)

Expand Down
3 changes: 2 additions & 1 deletion src/parameterList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const parameterDescriptions = {
requestTimeout: 'Request timeout - in seconds(default value is 120 seconds)',
resume: 'Do not overwrite if ZIM file already created',
speed: 'Multiplicator for the number of parallel HTTP requests on Parsoid backend (per default the number of CPU cores). The default value is 1.',
verbose: 'Print information to the stdout. Can be one of "info", "log", "warn", "error"',
verbose:
'Print information to the stdout if the level is "info" or "log", and to the stderr, if the level is warn or error. The option can be empty or one of "info", "log", "warn", "error", or "quiet". Option with an empty value is equal to "info".The default level is "error". If you choose the lower level then you will see messages also from the more high levels. For example, if you use warn then you will see warnings and errors.',
withoutZimFullTextIndex: "Don't include a fulltext search index to the ZIM",
webp: 'Convert all jpeg, png and gif images to webp format',
addNamespaces: 'Force additional namespace (comma separated numbers)',
Expand Down
11 changes: 10 additions & 1 deletion src/sanitize-argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ const __dirname = path.dirname(__filename)

export async function sanitize_all(argv: any) {
// extracting all arguments
const { articleList, addNamespaces, speed: _speed, adminEmail, mwUrl, customZimFavicon, optimisationCacheUrl, customZimLongDescription, customZimDescription } = argv
const { articleList, addNamespaces, speed: _speed, adminEmail, mwUrl, customZimFavicon, optimisationCacheUrl, verbose, customZimLongDescription, customZimDescription } = argv

sanitize_articlesList_addNamespaces(articleList, addNamespaces)

// sanitizing verbose
sanitize_verbose(verbose)

// sanitizing speed
sanitize_speed(_speed)

Expand Down Expand Up @@ -84,6 +87,12 @@ export function sanitizeStringMaxLength(text: string, key: string, length: numbe
}
}

export function sanitize_verbose(verbose: logger.LogLevel | true) {
if (verbose && verbose !== true && !logger.logLevels.includes(verbose)) {
throw new Error('verbose should be empty or one of [info, log, warn, error, quiet].')
}
}

export function sanitize_articlesList_addNamespaces(articlesList: string, addNamespaces: string) {
if (articlesList && addNamespaces) {
throw new Error('options --articlesList and --addNamespaces cannot be used together')
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/cmd.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ describe('Exec Command With Bash', () => {
/options --articlesList and --addNamespaces cannot be used together/,
)
})

test('Exec Command With --verbose option', async () => {
await expect(execa(`${mwo} --verbose=anyString --mwUrl="https://en.wikipedia.org" --adminEmail="test@test.test"`, { shell: true })).rejects.toThrow(
/verbose should be empty or one of \[info, log, warn, error, quiet\]/,
)
})

})
})
17 changes: 11 additions & 6 deletions test/unit/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('Logger', () => {
})

test('logger info level', async () => {
;(process as any).verbose = 'info'
logger.setVerboseLevel('info')

logger.info('test info', 'info test message')
logger.log('test log', 'log test message')
logger.warn('test warn', 'warn test message')
Expand All @@ -40,7 +41,8 @@ describe('Logger', () => {
})

test('logger log level', async () => {
;(process as any).verbose = 'log'
logger.setVerboseLevel('log')

logger.info('test info', 'info test message')
logger.log('test log', 'log test message')
logger.warn('test warn', 'warn test message')
Expand All @@ -53,7 +55,8 @@ describe('Logger', () => {
})

test('logger warn level', async () => {
;(process as any).verbose = 'warn'
logger.setVerboseLevel('warn')

logger.info('test info', 'info test message')
logger.log('test log', 'log test message')
logger.warn('test warn', 'warn test message')
Expand All @@ -66,7 +69,8 @@ describe('Logger', () => {
})

test('logger error level', async () => {
;(process as any).verbose = 'error'
logger.setVerboseLevel('error')

logger.info('test info', 'info test message')
logger.log('test log', 'log test message')
logger.warn('test warn', 'warn test message')
Expand All @@ -79,7 +83,8 @@ describe('Logger', () => {
})

test('logger verbose true', async () => {
;(process as any).verbose = true
logger.setVerboseLevel('true')

logger.info('test info', 'info test message')
logger.log('test log', 'log test message')
logger.warn('test warn', 'warn test message')
Expand All @@ -92,7 +97,7 @@ describe('Logger', () => {
})

test('logger verbose empty', async () => {
;(process as any).verbose = null
logger.setVerboseLevel(null)

logger.info('test info', 'info test message')
logger.log('test log', 'log test message')
Expand Down

0 comments on commit c3919c6

Please sign in to comment.