Skip to content

Commit

Permalink
Define log level enum in spdlog d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Nov 30, 2017
1 parent 227c20b commit a2f6308
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/typings/spdlog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ declare module 'spdlog' {
export const version: string;
export function setAsyncMode(bufferSize: number, flushInterval: number);

export enum LogLevel {
CRITICAL,
ERROR,
WARN,
INFO,
DEBUG,
TRACE,
OFF
}

export class RotatingLogger {
constructor(name: string, filename: string, filesize: number, filecount: number);

Expand All @@ -17,7 +27,7 @@ declare module 'spdlog' {
warn(message: string);
error(message: string);
critical(message: string);
setLevel(level: number);
setLevel(level: LogLevel);
flush(): void;
}
}
29 changes: 26 additions & 3 deletions src/vs/platform/log/node/spdlogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
'use strict';

import * as path from 'path';
import { ILogService } from 'vs/platform/log/common/log';
import { ILogService, LogLevel } from 'vs/platform/log/common/log';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { RotatingLogger, setAsyncMode } from 'spdlog';
import { RotatingLogger, setAsyncMode, LogLevel as SpdLogLevel } from 'spdlog';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { fromNodeEventEmitter } from 'vs/base/common/event';
import { TPromise } from 'vs/base/common/winjs.base';
Expand All @@ -29,7 +29,7 @@ export class SpdLogService implements ILogService {

const logfilePath = path.join(environmentService.logsPath, `${processName}.log`);
this.logger = new RotatingLogger(processName, logfilePath, 1024 * 1024 * 5, 6);
this.logger.setLevel(environmentService.logLevel);
this.setLevel(environmentService.logLevel);

fromNodeEventEmitter(process, 'exit')(() => this.logger.flush(), null, this.disposables);
}
Expand All @@ -48,6 +48,10 @@ export class SpdLogService implements ILogService {
await TPromise.join(toDelete.map(name => rimraf(path.join(logsRoot, name))));
}

setLevel(logLevel: LogLevel): void {
this.logger.setLevel(this.getLogLevel(logLevel));
}

// TODO, what about ARGS?
trace(message: string, ...args: any[]): void {
this.logger.trace(message);
Expand Down Expand Up @@ -77,4 +81,23 @@ export class SpdLogService implements ILogService {
dispose(): void {
this.disposables = dispose(this.disposables);
}

private getLogLevel(logLevel: LogLevel): SpdLogLevel {
switch (logLevel) {
case LogLevel.CRITICAL:
return SpdLogLevel.CRITICAL;
case LogLevel.ERROR:
return SpdLogLevel.ERROR;
case LogLevel.WARN:
return SpdLogLevel.WARN;
case LogLevel.INFO:
return SpdLogLevel.INFO;
case LogLevel.DEBUG:
return SpdLogLevel.DEBUG;
case LogLevel.TRACE:
return SpdLogLevel.TRACE;
case LogLevel.OFF:
return SpdLogLevel.OFF;
}
}
}

0 comments on commit a2f6308

Please sign in to comment.