Skip to content

Commit

Permalink
Extract Format interface from the console sink
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamboy1 committed May 18, 2020
1 parent 64118c6 commit 23b769a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
26 changes: 26 additions & 0 deletions format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** A parsed format */
export interface Format {
/** The original format string */
format: string;
/** The parsed string */
output: string;
/** A map of raw variables from the string */
variables: Map<string, unknown>;
}

/** Parse a string format with variables */
export const parseFormat = (format: string, ...args: unknown[]): Format => {
const variables = new Map<string, unknown>();

const output = format.replace(/\{(.*?)\}/g, (_, p1: string) => {
const arg = args.shift();
variables.set(p1, arg);
return `${arg}` || "";
});

return {
format,
output,
variables,
};
};
19 changes: 12 additions & 7 deletions logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2020 Yamboy1. All rights reserved. MIT license.

import { parseFormat } from "./format.ts";
import { LogLevel } from "./levels.ts";
import { Sink, consoleSink } from "./sinks.ts";

Expand All @@ -13,6 +14,8 @@ export function createLogger(
});
}

type LogFunction = (format: string, ...args: unknown[]) => void;

export interface LoggerOptions {
minimumLevel: LogLevel;
sinks: Sink[];
Expand All @@ -22,11 +25,11 @@ export interface Logger {
/** Add a sink at runtime, in most cases, the sinks parameter from createLogger should be used instead. */
addSink(sink: Sink): this;

debug(format: string, ...args: unknown[]): void;
info(format: string, ...args: unknown[]): void;
warning(format: string, ...args: unknown[]): void;
error(format: string, ...args: unknown[]): void;
critical(format: string, ...args: unknown[]): void;
debug: LogFunction;
info: LogFunction;
warning: LogFunction;
error: LogFunction;
critical: LogFunction;
}

class LoggerImpl {
Expand All @@ -43,17 +46,19 @@ class LoggerImpl {
return this;
}

private log(level: LogLevel, format: string, ...args: unknown[]): void {
private log(level: LogLevel, formatString: string, ...args: unknown[]): void {
if (level < this.minimumLevel) return;

const format = parseFormat(formatString, ...args);

if (this.sinks.length === 0) {
console.error(
"[Deno Structured Logging] Warning: No sinks are provided.",
);
}

for (let sink of this.sinks) {
sink(level, format, ...args);
sink(level, format);
}
}

Expand Down
42 changes: 14 additions & 28 deletions sinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,31 @@ import {
red,
bold,
} from "https://deno.land/[email protected]/fmt/colors.ts";
import { Format } from "./format.ts";
import { LogLevel } from "./levels.ts";

/** An output for the logger, sometimes known as a handler */
export type Sink = (
level: LogLevel,
format: string,
...args: unknown[]
format: Format,
) => void;

/** The default console sink */
export const consoleSink: Sink = (
level: LogLevel,
format: string,
...args: unknown[]
format: Format,
) => {
const message = format.replace(/\{.*?\}/g, () => `${args.shift()}` || "");
const timestamp = `[${new Date().toISOString()} ${LogLevel[level]}]`;
const message = `[${new Date().toISOString()} ${
LogLevel[level]
}] ${format.output}`;

let color;
switch (level) {
case LogLevel.DEBUG:
color = gray;
break;
case LogLevel.INFO:
color = white;
break;
case LogLevel.WARNING:
color = yellow;
break;
case LogLevel.ERROR:
color = red;
break;
case LogLevel.CRITICAL:
color = (str: string) => bold(red(str));
break;
default:
color = white;
break;
}
const color = ({
[LogLevel.DEBUG]: gray,
[LogLevel.INFO]: white,
[LogLevel.WARNING]: yellow,
[LogLevel.ERROR]: red,
[LogLevel.CRITICAL]: (str: string) => bold(red(str)),
})[level] ?? white;

console.log(color(`${timestamp} ${message}`));
console.log(color(message));
};

0 comments on commit 23b769a

Please sign in to comment.