Skip to content

Commit

Permalink
chore: use console-log-level for logging (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanmar511 authored Aug 15, 2018
1 parent bec5652 commit 3095737
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@google-cloud/common": "^0.20.3",
"bindings": "^1.2.1",
"console-log-level": "^1.4.0",
"delay": "^3.0.0",
"extend": "^3.0.1",
"gcp-metadata": "^0.7.0",
Expand All @@ -41,6 +42,7 @@
"semver": "^5.5.0"
},
"devDependencies": {
"@types/console-log-level": "^1.4.0",
"@types/delay": "^2.0.0",
"@types/extend": "^3.0.0",
"@types/long": "^4.0.0",
Expand Down
32 changes: 27 additions & 5 deletions ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import {Logger, util} from '@google-cloud/common';
import {util} from '@google-cloud/common';
import * as consoleLogLevel from 'console-log-level';
import * as delay from 'delay';
import * as extend from 'extend';
import * as fs from 'fs';
Expand Down Expand Up @@ -188,9 +189,26 @@ export async function start(config: Config = {}): Promise<void> {
profiler.start();
}

const LEVEL_NAMES: consoleLogLevel.LogLevelNames[] =
['fatal', 'error', 'warn', 'info', 'debug', 'trace'];

export function logLevelToName(level?: number): consoleLogLevel.LogLevelNames {
if (level === undefined) {
level = defaultConfig.logLevel;
} else if (level < 0) {
level = 0;
} else if (level > 4) {
level = 4;
}
return LEVEL_NAMES[level];
}

function logError(msg: string, config: Config) {
const logger =
new Logger({level: Logger.LEVELS[config.logLevel || 2], tag: pjson.name});
const logger = consoleLogLevel({
stderr: true,
prefix: pjson.name,
level: logLevelToName(config.logLevel)
});
logger.error(msg);
}

Expand All @@ -208,8 +226,12 @@ export async function startLocal(config: Config = {}): Promise<void> {
}

// Set up periodic logging.
const logger = new Logger(
{level: Logger.LEVELS[profiler.config.logLevel], tag: pjson.name});
const logger = consoleLogLevel({
stderr: true,
prefix: pjson.name,
level: logLevelToName(config.logLevel)
});

let heapProfileCount = 0;
let timeProfileCount = 0;
let prevLogTime = Date.now();
Expand Down
13 changes: 9 additions & 4 deletions ts/src/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import {Logger, Service, ServiceObject, util} from '@google-cloud/common';
import {Service, ServiceObject, util} from '@google-cloud/common';
import * as consoleLogLevel from 'console-log-level';
import * as http from 'http';
import * as path from 'path';
import * as pify from 'pify';
Expand All @@ -24,6 +25,7 @@ import * as zlib from 'zlib';
import {perftools} from '../../proto/profile';

import {ProfilerConfig} from './config';
import {logLevelToName} from './index';
import * as heapProfiler from './profilers/heap-profiler';
import {TimeProfiler} from './profilers/time-profiler';

Expand Down Expand Up @@ -232,7 +234,7 @@ function responseToProfileOrError(
* profiles can be collected.
*/
export class Profiler extends ServiceObject {
private logger: Logger;
private logger: consoleLogLevel.Logger;
private profileLabels: {instance?: string};
private deployment: Deployment;
private profileTypes: string[];
Expand All @@ -252,8 +254,11 @@ export class Profiler extends ServiceObject {
super({parent: new Service(serviceConfig, config), baseUrl: '/'});
this.config = config;

this.logger = new Logger(
{level: Logger.LEVELS[config.logLevel as number], tag: pjson.name});
this.logger = consoleLogLevel({
stderr: true,
prefix: pjson.name,
level: logLevelToName(this.config.logLevel)
});

const labels: {zone?: string,
version?: string, language: string} = {language: 'nodejs'};
Expand Down

0 comments on commit 3095737

Please sign in to comment.