Skip to content

Commit 656f6b8

Browse files
authored
make logger interface generic (#102)
1 parent cbed6e0 commit 656f6b8

14 files changed

Lines changed: 30 additions & 30 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nflx-spectator",
3-
"version": "3.0.10",
3+
"version": "3.0.11",
44
"license": "Apache-2.0",
55
"homepage": "https://github.com/Netflix/spectator-js",
66
"author": "Netflix Telemetry Engineering <netflix-atlas@googlegroups.com>",

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export class Config {
3939

4040
location: string;
4141
extra_common_tags: Tags;
42-
logger: Logger;
42+
logger: Logger<string>;
4343

44-
constructor(location: string = "udp", extra_common_tags: Tags = {}, logger: Logger = get_logger()) {
44+
constructor(location: string = "udp", extra_common_tags: Tags = {}, logger: Logger<string> = get_logger()) {
4545
this.location = this.calculate_location(location);
4646
this.extra_common_tags = this.calculate_extra_common_tags(extra_common_tags);
4747
this.logger = logger;

src/logger/logger.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ const levels: Record<string, number> = {
77
fatal: 60
88
};
99

10-
export interface Logger {
11-
trace: (message: string) => void;
12-
debug: (message: string) => void;
13-
info: (message: string) => void;
14-
warn: (message: string) => void;
15-
error: (message: string) => void;
16-
fatal: (message: string) => void;
17-
[key: string]: (message: string) => void;
18-
}
10+
export type Logger<T extends string | number | symbol> = {
11+
trace: (message: T) => void;
12+
debug: (message: T) => void;
13+
info: (message: T) => void;
14+
warn: (message: T) => void;
15+
error: (message: T) => void;
16+
fatal: (message: T) => void;
17+
[key: string]: (message: T) => void;
18+
};
1919

20-
export function get_logger(level_name?: string): Logger {
20+
export function get_logger(level_name?: string): Logger<string> {
2121
let level_filter: number;
2222
if (level_name == undefined || !(level_name in levels)) {
2323
level_filter = levels.info;
2424
} else {
2525
level_filter = levels[level_name];
2626
}
2727

28-
const logger: Logger = {
28+
const logger: Logger<string> = {
2929
is_level_enabled: (name: string) => levels[name] >= level_filter,
3030
trace: function (): void {
3131
throw new Error("Function not implemented.");

src/meter/id.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ export class Id {
2525

2626
private INVALID_CHARS: RegExp = new RegExp(/[^-._A-Za-z0-9~^]/g);
2727

28-
private readonly _logger: Logger;
28+
private readonly _logger: Logger<string>;
2929
private readonly _name: string;
3030
private readonly _tags: Tags;
3131
public spectatord_id: string;
3232

33-
constructor(name: string, tags?: Tags, logger?: Logger) {
33+
constructor(name: string, tags?: Tags, logger?: Logger<string>) {
3434
// initialization order in this constructor matters, for logging and testing purposes
3535
if (logger == undefined) {
3636
this._logger = get_logger();

src/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class Registry {
2020
* Registry is the main entry point for interacting with the Spectator library.
2121
*/
2222

23-
public logger: Logger;
23+
public logger: Logger<string>;
2424

2525
private _config: Config;
2626
private readonly _writer: WriterUnion;

src/writer/file_writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class FileWriter extends Writer {
1010

1111
private readonly _location: string;
1212

13-
constructor(location: string, logger: Logger = get_logger()) {
13+
constructor(location: string, logger: Logger<string> = get_logger()) {
1414
super(logger);
1515
// convert from URL string to PathLike string
1616
this._location = fileURLToPath(location);

src/writer/memory_writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class MemoryWriter extends Writer {
1313

1414
private _messages: string[];
1515

16-
constructor(logger: Logger = get_logger()) {
16+
constructor(logger: Logger<string> = get_logger()) {
1717
super(logger);
1818
this._logger.debug("initialize MemoryWriter");
1919
this._messages = [];

src/writer/new_writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function is_valid_output_location(location: string): boolean {
1515
location.startsWith("udp://");
1616
}
1717

18-
export function new_writer(location: string, logger?: Logger): WriterUnion {
18+
export function new_writer(location: string, logger?: Logger<string>): WriterUnion {
1919
/**
2020
* Create a new Writer based on an output location.
2121
*/

src/writer/noop_writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class NoopWriter extends Writer {
66
* Writer that does nothing. Used to disable output.
77
*/
88

9-
constructor(logger: Logger = get_logger()) {
9+
constructor(logger: Logger<string> = get_logger()) {
1010
super(logger);
1111
this._logger.debug("initialize NoopWriter");
1212
}

src/writer/stderr_writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class StderrWriter extends Writer {
66
* Writer that outputs data to stderr.
77
*/
88

9-
constructor(logger: Logger = get_logger()) {
9+
constructor(logger: Logger<string> = get_logger()) {
1010
super(logger);
1111
this._logger.debug("initialize StderrWriter");
1212
}

0 commit comments

Comments
 (0)