Skip to content

Commit

Permalink
fix: better logger types
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 authored and edvald committed Apr 22, 2018
1 parent 195f700 commit 56596fb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bin/integ
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ garden_bin=${garden_root}/build/src/bin/garden.js

chmod +x ${garden_bin}

export GARDEN_LOGGER_TYPE=test
export GARDEN_LOGGER_TYPE=basic

cd ${garden_root}/examples/hello-world

Expand Down
24 changes: 14 additions & 10 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ import {
LogEntryOpts,
LogSymbolType,
} from "./types"
import { FancyConsoleWriter, Writer } from "./writers"
import { FancyConsoleWriter, Writer, BasicConsoleWriter } from "./writers"
import { ParameterError } from "../exceptions"

const ROOT_DEPTH = -1
const DEFAULT_CONFIGS: {[key in LoggerType]: LoggerConfig} = {
[LoggerType.development]: {
[LoggerType.fancy]: {
level: LogLevel.info,
writers: [new FancyConsoleWriter()],
},
[LoggerType.test]: {
level: LogLevel.error,
[LoggerType.basic]: {
level: LogLevel.info,
writers: [new BasicConsoleWriter()],
},
[LoggerType.quiet]: {
level: LogLevel.info,
},
}

Expand All @@ -50,7 +54,7 @@ export interface LogEntryConstructor {
}

let loggerInstance: RootLogNode
let defaultLoggerType: LoggerType = LoggerType.development
let defaultLoggerType: LoggerType = LoggerType.fancy
let defaultLoggerConfig: LoggerConfig = DEFAULT_CONFIGS[defaultLoggerType]

function createLogEntry(level: LogLevel, opts: LogEntryOpts, parent: LogNode) {
Expand Down Expand Up @@ -208,8 +212,8 @@ export class LogEntry extends LogNode {
return this
}

public originIsNotLogger(): boolean {
return !!this.opts.originIsNotLogger
public notOriginatedFromLogger(): boolean {
return !!this.opts.notOriginatedFromLogger
}

public stop() {
Expand Down Expand Up @@ -244,7 +248,7 @@ export class RootLogNode extends LogNode {
}

public getLogEntries(): LogEntry[] {
return getChildNodes(<any>this).filter(entry => !entry.originIsNotLogger())
return getChildNodes(<any>this).filter(entry => !entry.notOriginatedFromLogger())
}

public header(
Expand Down Expand Up @@ -278,9 +282,9 @@ export class RootLogNode extends LogNode {

}

export function getLogger(config: LoggerConfig = defaultLoggerConfig) {
export function getLogger(config?: LoggerConfig) {
if (!loggerInstance) {
loggerInstance = new RootLogNode(config)
loggerInstance = new RootLogNode(config || defaultLoggerConfig)
}

return loggerInstance
Expand Down
7 changes: 4 additions & 3 deletions src/logger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export enum LogLevel {
}

export enum LoggerType {
development = "development",
test = "test",
basic = "basic",
fancy = "fancy",
quiet = "quiet",
}

// Defines entry style and format (only one available style at the moment)
Expand Down Expand Up @@ -62,7 +63,7 @@ export interface LogEntryOpts {
symbol?: LogSymbolType
entryStyle?: EntryStyle
append?: boolean
originIsNotLogger?: boolean
notOriginatedFromLogger?: boolean
showDuration?: boolean
error?: GardenError | Error
}
2 changes: 1 addition & 1 deletion test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { setDefaultLoggerType } from "../src/logger"

// Global before hooks
before(() => {
setDefaultLoggerType(LoggerType.test)
setDefaultLoggerType(LoggerType.quiet)
})

0 comments on commit 56596fb

Please sign in to comment.