Skip to content

Commit

Permalink
fix: do not export console-log-level publicly (#337)
Browse files Browse the repository at this point in the history
* fix: do not export console-log-level publicly

console-log-level types are an implementation detail. Exporting them
meant that users needed to have @types/console-log-level installed to be
able to use this module. This requires either that the users know that
they need @types/console-log-level in their devDependencies or we need
to decalare them in our main dependencies.

Fixes: #336

* Update ts/src/logger.ts
  • Loading branch information
ofrobots authored and nolanmar511 committed Nov 20, 2018
1 parent 88f71a4 commit 4c61da5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
29 changes: 4 additions & 25 deletions ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import * as consoleLogLevel from 'console-log-level';
import delay from 'delay';
import * as extend from 'extend';
import * as fs from 'fs';
Expand All @@ -23,6 +22,7 @@ import * as semver from 'semver';
import {SemVer} from 'semver';

import {Config, defaultConfig, ProfilerConfig} from './config';
import {createLogger} from './logger';
import {Profiler} from './profiler';
import * as heapProfiler from './profilers/heap-profiler';

Expand Down Expand Up @@ -191,26 +191,9 @@ 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 = consoleLogLevel({
stderr: true,
prefix: pjson.name,
level: logLevelToName(config.logLevel)
});
// FIXME: do not create a new logger on each error.
const logger = createLogger(config.logLevel);
logger.error(msg);
}

Expand All @@ -228,11 +211,7 @@ export async function startLocal(config: Config = {}): Promise<void> {
}

// Set up periodic logging.
const logger = consoleLogLevel({
stderr: true,
prefix: pjson.name,
level: logLevelToName(config.logLevel)
});
const logger = createLogger(config.logLevel);

let heapProfileCount = 0;
let timeProfileCount = 0;
Expand Down
39 changes: 39 additions & 0 deletions ts/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as consoleLogLevel from 'console-log-level';
import {defaultConfig} from './config';

const pjson = require('../../package.json');

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

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];
}

export function createLogger(level?: number) {
return consoleLogLevel(
{stderr: true, prefix: pjson.name, level: logLevelToName(level)});
}
11 changes: 3 additions & 8 deletions ts/src/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

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

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

Expand Down Expand Up @@ -247,7 +246,7 @@ function responseToProfileOrError(
* profiles can be collected.
*/
export class Profiler extends ServiceObject {
private logger: consoleLogLevel.Logger;
private logger: ReturnType<typeof createLogger>;
private profileLabels: {instance?: string};
private deployment: Deployment;
private profileTypes: string[];
Expand All @@ -274,11 +273,7 @@ export class Profiler extends ServiceObject {
});
this.config = config;

this.logger = consoleLogLevel({
stderr: true,
prefix: pjson.name,
level: logLevelToName(this.config.logLevel)
});
this.logger = createLogger(this.config.logLevel);

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

0 comments on commit 4c61da5

Please sign in to comment.