Skip to content

Commit

Permalink
fix: silent log level does not work (unjs#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr authored and himanshiLt committed Jun 20, 2022
1 parent 9cbcc83 commit 15066d8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/consola.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Consola {
let max = 0
for (const typeName in this._types) {
const type = this._types[typeName]
if (type.level === Infinity || type.level === -Infinity) {
continue
}
if (type.level > max) {
max = type.level
} else if (type.level < min) {
Expand Down
3 changes: 2 additions & 1 deletion src/logLevels.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ LogLevel[LogLevel.Info = 3] = 'Info'
LogLevel[LogLevel.Success = 3] = 'Success'
LogLevel[LogLevel.Debug = 4] = 'Debug'
LogLevel[LogLevel.Trace = 5] = 'Trace'
LogLevel[LogLevel.Silent = Infinity] = 'Silent'
LogLevel[LogLevel.Silent = -Infinity] = 'Silent'
LogLevel[LogLevel.Verbose = Infinity] = 'Verbose'
4 changes: 4 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default {
silent: {
level: LogLevel.Silent
},
// Enable all log levels
verbose: {
level: LogLevel.Verbose
},

// Legacy
ready: {
Expand Down
27 changes: 26 additions & 1 deletion test/consola.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Consola } from '../src'
import { Consola, LogLevel } from '../src'

describe('consola', () => {
test('can set level', () => {
Expand All @@ -16,6 +16,31 @@ describe('consola', () => {

consola.level = -99
expect(consola.level).toBe(0)

consola.level = 99
expect(consola.level).toBe(5)
})

test('silent log level does\'t print logs', async () => {
const logs = []
class TestReporter {
log (logObj) {
logs.push(logObj)
}
}

const consola = new Consola({
throttle: 100,
level: LogLevel.Silent,
reporters: [
new TestReporter()
]
})
for (let i = 0; i < 10; i++) {
consola.log('SPAM')
}
await wait(200)
expect(logs.length).toBe(0)
})

test('can see spams without ending log', async () => {
Expand Down
33 changes: 21 additions & 12 deletions types/consola.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export enum LogLevel {
Success= 3,
Debug= 4,
Trace= 5,
Silent= Infinity,
Silent= -Infinity,
Verbose= Infinity,
}

export interface ConsolaLogObject {
Expand All @@ -22,6 +23,14 @@ export interface ConsolaLogObject {
date?: Date,
}

export interface ConsolaReporterLogObject {
level: LogLevel,
type: string,
tag: string;
args: any[],
date: Date,
}

type ConsolaMock = (...args: any) => void

type ConsolaMockFn = (type: string, defaults: ConsolaLogObject) => ConsolaMock
Expand All @@ -33,7 +42,7 @@ export interface ConsolaReporterArgs {
}

export interface ConsolaReporter {
log: (logObj: ConsolaLogObject, args: ConsolaReporterArgs) => void
log: (logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs) => void
}

export interface ConsolaOptions {
Expand All @@ -42,8 +51,8 @@ export interface ConsolaOptions {
level?: LogLevel,
defaults?: ConsolaLogObject,
async?: boolean,
stdout?: any,
stderr?: any,
stdout?: NodeJS.WritableStream,
stderr?: NodeJS.WritableStream,
mockFn?: ConsolaMockFn,
throttle?: number,
}
Expand All @@ -52,8 +61,8 @@ export declare class Consola {
constructor(options: ConsolaOptions)

level: LogLevel
readonly stdout: any
readonly stderr: any
readonly stdout: NodeJS.WritableStream
readonly stderr: NodeJS.WritableStream

// Built-in log levels
fatal(message: ConsolaLogObject | any, ...args: any[]): void
Expand Down Expand Up @@ -112,13 +121,13 @@ export declare class BasicReporter implements ConsolaReporter {

constructor(options?: BasicReporterOptions);

public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;

protected formatStack(stack: string): string;
protected formatArgs(args: any[]): string;
protected formatDate(date: Date): string;
protected filterAndJoin(arr: Array<string | undefined>): string;
protected formatLogObj(logObj: ConsolaLogObject): string;
protected formatLogObj(logObj: ConsolaReporterLogObject): string;
}

export interface FancyReporterOptions extends BasicReporterOptions{
Expand All @@ -128,13 +137,13 @@ export interface FancyReporterOptions extends BasicReporterOptions{
export declare class FancyReporter extends BasicReporter {
constructor(options?: FancyReporterOptions);

protected formatType(logObj: ConsolaLogObject): void;
protected formatType(logObj: ConsolaReporterLogObject): void;
}

export type BrowserReporterOptions = {};

export declare class BrowserReporter implements ConsolaReporter {
public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;
}

export type JSONReporterOptions = {
Expand All @@ -143,14 +152,14 @@ export type JSONReporterOptions = {

export declare class JSONReporter implements ConsolaReporter {
constructor(options?: JSONReporterOptions);
public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;
}

export type Winston = any;

export declare class WinstonReporter implements ConsolaReporter {
constructor(logger?: Winston);
public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;
}

declare const consolaGlobalInstance: Consola;
Expand Down

0 comments on commit 15066d8

Please sign in to comment.