Skip to content

Commit

Permalink
fix(logger): only inherit parent level if option is set (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Jan 29, 2019
1 parent b1e8fa6 commit 99fdb12
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion garden-service/src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class LogsCommand extends Command<Args, Opts> {
})

await Bluebird.map(services, async (service: Service<any>) => {
const voidLog = log.placeholder(LogLevel.silly)
const voidLog = log.placeholder(LogLevel.silly, { preserveLevel: true })
const status = await garden.actions.getServiceStatus({ log: voidLog, service, hotReload: false })

if (status.state === "ready" || status.state === "outdated") {
Expand Down
15 changes: 10 additions & 5 deletions garden-service/src/logger/log-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as nodeEmoji from "node-emoji"
import { flatten } from "lodash"

import { LogNode, LogLevel } from "./log-node"
import { getChildEntries } from "./util"
import { getChildEntries, findParentEntry } from "./util"
import { GardenError } from "../exceptions"
import { Omit } from "../util/util"
import { Logger } from "./logger"
Expand All @@ -31,6 +31,7 @@ export interface UpdateOpts {
error?: GardenError
status?: EntryStatus
indent?: number
preserveLevel?: boolean
}

export interface CreateOpts extends UpdateOpts {
Expand Down Expand Up @@ -104,12 +105,16 @@ export class LogEntry extends LogNode {
}

protected createNode(level: LogLevel, param: CreateParam) {
// A child entry must not have a higher level than its parent
const childLevel = this.level > level ? this.level : level
const opts = {
indent: (this.opts.indent || 0) + 1,
...resolveParam(param),
}

// If preserveLevel is set to true, all children must have a level geq the level
// of the parent entry that set the flag.
const parentWithPreserveFlag = findParentEntry(this, entry => !!entry.opts.preserveLevel)
const childLevel = parentWithPreserveFlag ? Math.max(parentWithPreserveFlag.level, level) : level

return new LogEntry({
opts,
level: childLevel,
Expand All @@ -122,10 +127,10 @@ export class LogEntry extends LogNode {
this.root.onGraphChange(node)
}

placeholder(level: LogLevel = LogLevel.info): LogEntry {
placeholder(level: LogLevel = LogLevel.info, param? : CreateParam): LogEntry {
// Ensure placeholder child entries align with parent context
const indent = Math.max((this.opts.indent || 0) - 1, - 1)
return this.appendNode(level, { indent })
return this.appendNode(level, { ...resolveParam(param), indent })
}

// Preserves status
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/logger/log-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export abstract class LogNode {
* A placeholder entry is an empty entry whose children should be aligned with the parent context.
* Useful for setting a placeholder in the middle of the log that can later be populated.
*/
abstract placeholder(level: LogLevel): LogEntry
abstract placeholder(level: LogLevel, param?: CreateParam): LogEntry

protected appendNode(level: LogLevel, param: CreateParam): LogEntry {
const node = this.createNode(level, param)
Expand Down
6 changes: 6 additions & 0 deletions garden-service/src/logger/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export function getChildEntries(node: LogNode): LogEntry[] {
return getChildNodes<LogNode, LogEntry>(node)
}

export function findParentEntry(entry: LogEntry, predicate: ProcessNode<LogEntry>): LogEntry | null {
return predicate(entry)
? entry
: entry.parent ? findParentEntry(entry.parent, predicate) : null
}

export function findLogNode(node: LogNode, predicate: ProcessNode<LogNode>): LogEntry | void {
let found: LogEntry | undefined
traverseChildren<LogNode, LogEntry>(node, entry => {
Expand Down

0 comments on commit 99fdb12

Please sign in to comment.