fix(docutils): fix the docutils logger

Proxy wasn't working properly. Now it is
This commit is contained in:
Christopher Hiller
2023-02-06 13:47:04 -08:00
parent 258860f377
commit cd72da3b3f
3 changed files with 36 additions and 30 deletions

View File

@@ -1,23 +1,15 @@
#!/usr/bin/env node
import logger from '../logger';
import {LogLevel} from 'consola';
import _ from 'lodash';
import {hideBin} from 'yargs/helpers';
import yargs from 'yargs/yargs';
import {build, init, validate} from './command';
import {DEFAULT_LOG_LEVEL, LogLevelMap, NAME_BIN} from '../constants';
import {DocutilsError} from '../error';
import log from '../logger';
import {DEFAULT_LOG_LEVEL, NAME_BIN} from '../constants';
const LogLevelName = {
silent: LogLevel.Silent,
error: LogLevel.Error,
warn: LogLevel.Warn,
info: LogLevel.Info,
debug: LogLevel.Debug,
} as const;
import {build, init, validate} from './command';
import {findConfig} from './config';
const log = logger.withTag('cli');
export async function main(argv = hideBin(process.argv)) {
const config = await findConfig(argv);
@@ -29,16 +21,15 @@ export async function main(argv = hideBin(process.argv)) {
.command(validate)
.options({
verbose: {
alias: 'V',
type: 'boolean',
describe: 'Alias for --log-level=debug',
},
'log-level': {
alias: 'L',
choices: ['debug', 'info', 'warn', 'error', 'silent'],
default: DEFAULT_LOG_LEVEL,
describe: 'Sets the log level',
coerce: _.identity as (x: string) => keyof typeof LogLevelName,
default: DEFAULT_LOG_LEVEL,
coerce: _.identity as (x: string) => keyof typeof LogLevelMap,
},
config: {
alias: 'c',
@@ -59,12 +50,11 @@ export async function main(argv = hideBin(process.argv)) {
* Configures logging; `--verbose` implies `--log-level=debug`
*/
(argv) => {
const {logLevel, verbose} = argv;
if (verbose) {
if (argv.verbose) {
argv.logLevel = 'debug';
log.debug('Debug logging enabled via --verbose');
}
log.level = LogLevelName[logLevel];
log.level = LogLevelMap[argv.logLevel];
}
)
.fail(

View File

@@ -3,6 +3,7 @@
* @module
*/
import {LogLevel} from 'consola';
import {readFileSync} from 'node:fs';
import {fs} from '@appium/support';
import path from 'node:path';
@@ -107,3 +108,14 @@ export const REQUIREMENTS_TXT_PATH = path.join(PKG_ROOT_DIR, NAME_REQUIREMENTS_T
* The default output path for Typedoc, computed relative to the consuming package's root
*/
export const DEFAULT_REL_TYPEDOC_OUT_PATH = path.join('docs', 'reference');
/**
* Mapping of `@appium/docutils`' log levels to `consola` log levels
*/
export const LogLevelMap = {
silent: LogLevel.Silent,
error: LogLevel.Error,
warn: LogLevel.Warn,
info: LogLevel.Info,
debug: LogLevel.Debug,
} as const;

View File

@@ -8,7 +8,14 @@
import consola from 'consola';
import type {Consola, ConsolaOptions, LogLevel} from 'consola';
import {DEFAULT_LOG_LEVEL} from './constants';
import {DEFAULT_LOG_LEVEL, LogLevelMap} from './constants';
/**
* The global log level
*
* "Global" inasmuch as any logger created from the root logger will use this level.
*/
let globalLevel = LogLevelMap[DEFAULT_LOG_LEVEL];
/**
* The logger from which all loggers are created
@@ -16,14 +23,7 @@ import {DEFAULT_LOG_LEVEL} from './constants';
* `withTag`/`withScope` is a way to namespace logs. This is more useful if using log objects, but
* you can also see the scope when using the CLI app (if your terminal window is 80+ cols).
*/
const rootLogger = consola.withTag('appium:docutils');
/**
* The global log level
*
* "Global" inasmuch as any logger created from the root logger will use this level.
*/
let globalLevel = rootLogger.level ?? DEFAULT_LOG_LEVEL;
const rootLogger = createLogProxy(consola.withTag('docutils'));
/**
* @summary Creates a log-level-propagating proxy for a {@linkcode Consola} logger.
@@ -42,16 +42,20 @@ let globalLevel = rootLogger.level ?? DEFAULT_LOG_LEVEL;
*
* There are other ways to go about this which may be better, but this seemed pretty straightforward.
*/
function createLogProxy(logger: Consola) {
function createLogProxy(logger: Consola): Consola {
return new Proxy(logger, {
get(target, prop, receiver) {
if (prop === 'level') {
return globalLevel ?? Reflect.get(target, prop, receiver);
return globalLevel;
}
if (prop === 'create') {
const create = Reflect.get(target, prop, receiver) as Consola['create'];
return (opts: ConsolaOptions) => createLogProxy(create.call(receiver, opts));
}
if (prop === '_defaults') {
const defaults = Reflect.get(target, prop, receiver);
return {...defaults, level: globalLevel};
}
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
@@ -68,4 +72,4 @@ function createLogProxy(logger: Consola) {
* The proxied root logger
* @see {createLogProxy}
*/
export default createLogProxy(rootLogger);
export default rootLogger;