Skip to content

Commit

Permalink
feat: support literal for logLevels (#133)
Browse files Browse the repository at this point in the history
close #130
  • Loading branch information
antfu authored Feb 25, 2022
1 parent 5d291fb commit 22738ac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/consola.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Types from './types.js'
import { isLogObj } from './utils/index.js'
import { normalizeLogLevel } from './logLevels'
import Types from './types'
import { isLogObj } from './utils/index'

let paused = false
const queue = []
Expand All @@ -8,7 +9,7 @@ class Consola {
constructor (options = {}) {
this._reporters = options.reporters || []
this._types = options.types || Types
this.level = options.level !== undefined ? options.level : 3
this._level = normalizeLogLevel(options.level, this._types)
this._defaults = options.defaults || {}
this._async = options.async !== undefined ? options.async : undefined
this._stdout = options.stdout
Expand Down Expand Up @@ -41,6 +42,14 @@ class Consola {
this._throttleTimeout = undefined
}

get level () {
return this._level
}

set level (level) {
this._level = normalizeLogLevel(level, this._types)
}

get stdout () {
return this._stdout || console._stdout // eslint-disable-line no-console
}
Expand Down
7 changes: 7 additions & 0 deletions src/logLevels.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ LogLevel[LogLevel.Debug = 4] = 'Debug'
LogLevel[LogLevel.Trace = 5] = 'Trace'
LogLevel[LogLevel.Silent = -Infinity] = 'Silent'
LogLevel[LogLevel.Verbose = Infinity] = 'Verbose'

export function normalizeLogLevel (input, types = {}, defaultLevel = 3) {
if (input == null) { return defaultLevel }
if (typeof input === 'number') { return input }
if (types[input] && types[input].level != null) { return types[input].level }
return defaultLevel
}
4 changes: 2 additions & 2 deletions test/consola.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Consola, LogLevel } from '../src'
import { Consola } from '../src'

describe('consola', () => {
test('can set level', () => {
Expand All @@ -21,7 +21,7 @@ describe('consola', () => {

const consola = new Consola({
throttle: 100,
level: LogLevel.Silent,
level: 'silent',
reporters: [
new TestReporter()
]
Expand Down
4 changes: 3 additions & 1 deletion types/consola.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export enum LogLevel {
Verbose= Infinity,
}

export type LogLevelLiterals = 'fatal' | 'error' | 'warn' | 'log' | 'info' | 'success' | 'debug' | 'trace' | 'silent' | 'verbose'

export type logType =
| 'silent'
| 'fatal'
Expand All @@ -28,7 +30,7 @@ export type logType =
| 'start'

export interface ConsolaLogObject {
level?: LogLevel,
level?: LogLevel | LogLevelLiterals,
tag?: string,
type?: logType,
message?: string,
Expand Down

0 comments on commit 22738ac

Please sign in to comment.