Skip to content

✨ Feature(log): add log level type and export default logPat… #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export { PicGo } from './core/PicGo'
export { Lifecycle } from './core/Lifecycle'
export * from './core/PicGo'
export * from './core/Lifecycle'

export { Logger } from './lib/Logger'
export { PluginHandler } from './lib/PluginHandler'
export { LifecyclePlugins } from './lib/LifecyclePlugins'
export { Commander } from './lib/Commander'
export { PluginLoader } from './lib/PluginLoader'
export { Request } from './lib/Request'
export * from './lib/Logger'
export * from './lib/PluginHandler'
export * from './lib/LifecyclePlugins'
export * from './lib/Commander'
export * from './lib/PluginLoader'
export * from './lib/Request'

export * from './types'
19 changes: 13 additions & 6 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
IPicGo
} from '../types'

// Convert enum into union, see: https://stackoverflow.com/a/52396706
export type LogLevel = `${ILogType}` | 'all'

export class Logger implements ILogger {
private readonly level = {
[ILogType.success]: 'green',
Expand All @@ -22,19 +25,23 @@ export class Logger implements ILogger {
}

private readonly ctx: IPicGo
private logLevel!: string
private logPath!: string
constructor (ctx: IPicGo) {
this.ctx = ctx
}

get logPath (): string {
return this.ctx.getConfig<Undefinable<string>>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log')
}

get logLevel (): LogLevel | LogLevel[] {
return this.ctx.getConfig<Undefinable<LogLevel>>('settings.logLevel') ?? 'all'
}

private handleLog (type: ILogType, ...msg: ILogArgvTypeWithError[]): void {
// check config.silent
if (!this.ctx.getConfig<Undefinable<string>>('silent')) {
const logHeader = chalk[this.level[type] as ILogColor](`[PicGo ${type.toUpperCase()}]:`)
console.log(logHeader, ...msg)
this.logLevel = this.ctx.getConfig('settings.logLevel')
this.logPath = this.ctx.getConfig<Undefinable<string>>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log')
setTimeout(() => {
this.handleWriteLog(this.logPath, type, ...msg)
}, 0)
Expand Down Expand Up @@ -64,12 +71,12 @@ export class Logger implements ILogger {
}
}

private checkLogLevel (type: string, level: undefined | string | string[]): boolean {
private checkLogLevel (type: string, level?: LogLevel | LogLevel[]): boolean {
if (level === undefined || level === 'all') {
return true
}
if (Array.isArray(level)) {
return level.some((item: string) => (item === type || item === 'all'))
return level.some((item) => (item === type || item === 'all'))
} else {
return type === level
}
Expand Down
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequestPromiseAPI } from 'request-promise-native'
import { Command } from 'commander'
import { Inquirer } from 'inquirer'
import { LogLevel } from '..'

export interface IPicGo extends NodeJS.EventEmitter {
/**
Expand Down Expand Up @@ -298,7 +299,7 @@ export interface IConfig {
debug?: boolean
silent?: boolean
settings?: {
logLevel?: string
logLevel?: LogLevel | LogLevel[]
logPath?: string
/** for npm */
registry?: string
Expand Down