Skip to content

Commit

Permalink
feat: add truncatePrevious option to file-writer
Browse files Browse the repository at this point in the history
Root error.log is now truncated on each cli run. Moved development.log
to the now default .garden/logs directory and added a persistent
error.log there as well
  • Loading branch information
eysi09 committed May 27, 2018
1 parent 02b05b3 commit a64fbb0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
21 changes: 18 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,30 @@ export class GardenCli {
const root = resolve(process.cwd(), optsForAction.root)
const env = optsForAction.env

// Update logger
// Configure logger
const logger = this.logger
const { loglevel, silent, output } = optsForAction
const level = LogLevel[<string>loglevel]
logger.level = level
if (!silent && !output) {
logger.writers.push(
new FileWriter({ level, root }),
new FileWriter({ level: LogLevel.error, filename: ERROR_LOG_FILENAME, root }),
await FileWriter.factory({
root,
level,
filename: "development.log",
}),
await FileWriter.factory({
root,
filename: ERROR_LOG_FILENAME,
level: LogLevel.error,
}),
await FileWriter.factory({
root,
logDirPath: ".",
filename: ERROR_LOG_FILENAME,
level: LogLevel.error,
truncatePrevious: true,
}),
)
} else {
logger.writers = []
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { resolve } from "path"
export const MODULE_CONFIG_FILENAME = "garden.yml"
export const STATIC_DIR = resolve(__dirname, "..", "static")
export const GARDEN_DIR_NAME = ".garden"
export const LOGS_DIR = `${GARDEN_DIR_NAME}/logs`
export const GARDEN_VERSIONFILE_NAME = ".garden-version"
export const DEFAULT_NAMESPACE = "default"
export const DEFAULT_PORT_PROTOCOL = "TCP"
Expand Down
34 changes: 27 additions & 7 deletions src/logger/writers/file-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import * as winston from "winston"
import * as path from "path"
import * as stripAnsi from "strip-ansi"
import { ensureDir, truncate } from "fs-extra"

import {
LogLevel,
Expand All @@ -20,17 +21,19 @@ import {
renderError,
renderMsg,
} from "../renderers"
import { LOGS_DIR } from "../../constants"

export interface FileWriterConfig {
level: LogLevel
root: string
filename?: string
filename: string
logDirPath?: string
fileTransportOptions?: {}
truncatePrevious?: boolean
}

const { combine: winstonCombine, timestamp, printf } = winston.format

const DEFAULT_LOG_FILENAME = "development.log"
const DEFAULT_FILE_TRANSPORT_OPTIONS = {
format: winstonCombine(
timestamp(),
Expand All @@ -47,12 +50,10 @@ export class FileWriter extends Writer {

public level: LogLevel

constructor(config: FileWriterConfig) {
constructor(filePath: string, config: FileWriterConfig) {
const {
fileTransportOptions = DEFAULT_FILE_TRANSPORT_OPTIONS,
filename = DEFAULT_LOG_FILENAME,
level,
root,
} = config

super({ level })
Expand All @@ -62,15 +63,34 @@ export class FileWriter extends Writer {
transports: [
new winston.transports.File({
...fileTransportOptions,
filename: path.join(root, filename),
filename: filePath,
}),
],
})
}

static async factory(config: FileWriterConfig) {
const {
filename,
root,
truncatePrevious,
logDirPath = LOGS_DIR,
} = config
const fullPath = path.join(root, logDirPath)
await ensureDir(fullPath)
const filePath = path.join(fullPath, filename)
if (truncatePrevious) {
try {
await truncate(filePath)
} catch (_) {
}
}
return new FileWriter(filePath, config)
}

render(entry: LogEntry): string | null {
const renderFn = entry.level === LogLevel.error ? renderError : renderMsg
if (validate(this.level, entry)) {
const renderFn = entry.level === LogLevel.error ? renderError : renderMsg
return stripAnsi(renderFn(entry))
}
return null
Expand Down

0 comments on commit a64fbb0

Please sign in to comment.