Skip to content

Commit

Permalink
refactor(rstream-log): update to use base types from @thi.ng/api pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 24, 2019
1 parent 8a87bd0 commit ef9bf8d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 56 deletions.
28 changes: 4 additions & 24 deletions packages/rstream-log/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
import { IID } from "@thi.ng/api";
import { IID, ILogger as APILogger, LogLevel } from "@thi.ng/api";
import { ISubscribable } from "@thi.ng/rstream";

export enum Level {
FINE,
DEBUG,
INFO,
WARN,
SEVERE,
NONE
}

/**
* Reverse lookup for `Level` enums
*/
// export const __Level = (<any>exports).Level;

export interface LogEntry extends Array<any> {
[0]: Level;
[0]: LogLevel;
[1]: string;
[2]: number;
[id: number]: any;
}

export interface LogEntryObj extends IID<string> {
level: Level;
level: LogLevel;
time: number;
body: any[];
}

export interface ILogger extends ISubscribable<LogEntry> {
fine(...args: any[]);
debug(...args: any[]);
info(...args: any[]);
warn(...args: any[]);
severe(...args: any[]);
}
export interface ILogger extends APILogger, ISubscribable<LogEntry> {}

export type DateFormat = (epoch: number) => string;
export type BodyFormat = (body: any[]) => string;
50 changes: 27 additions & 23 deletions packages/rstream-log/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { LogLevel } from "@thi.ng/api";
import { illegalArity } from "@thi.ng/errors";
import { ISubscribable, nextID, StreamMerge } from "@thi.ng/rstream";
import { ILogger, Level, LogEntry } from "./api";
import { ILogger, LogEntry } from "./api";

export class Logger extends StreamMerge<LogEntry, LogEntry> implements ILogger {
level: Level;
level: LogLevel;

constructor();
constructor(id: string);
constructor(id: string, level: Level);
constructor(id: string, level: LogLevel);
constructor(
id: string,
sources: Iterable<ISubscribable<LogEntry>>,
level?: Level
level?: LogLevel
);
constructor(...args: any[]) {
let id;
let level = Level.FINE;
let level = LogLevel.FINE;
let src;
switch (args.length) {
case 0:
Expand All @@ -39,38 +40,41 @@ export class Logger extends StreamMerge<LogEntry, LogEntry> implements ILogger {
}

next(x: LogEntry) {
if (x[0] >= this.level) {
super.next(x);
}
x[0] >= this.level && super.next(x);
}

fine(...args: any[]) {
if (this.level <= Level.FINE) {
this.next(<LogEntry>[Level.FINE, this.id, Date.now(), ...args]);
}
this.level <= LogLevel.FINE &&
super.next(<LogEntry>[LogLevel.FINE, this.id, Date.now(), ...args]);
}

debug(...args: any[]) {
if (this.level <= Level.DEBUG) {
this.next(<LogEntry>[Level.DEBUG, this.id, Date.now(), ...args]);
}
this.level <= LogLevel.DEBUG &&
super.next(<LogEntry>[
LogLevel.DEBUG,
this.id,
Date.now(),
...args
]);
}

info(...args: any[]) {
if (this.level <= Level.INFO) {
this.next(<LogEntry>[Level.INFO, this.id, Date.now(), ...args]);
}
this.level <= LogLevel.INFO &&
super.next(<LogEntry>[LogLevel.INFO, this.id, Date.now(), ...args]);
}

warn(...args: any[]) {
if (this.level <= Level.WARN) {
this.next(<LogEntry>[Level.WARN, this.id, Date.now(), ...args]);
}
this.level <= LogLevel.WARN &&
super.next(<LogEntry>[LogLevel.WARN, this.id, Date.now(), ...args]);
}

severe(...args: any[]) {
if (this.level <= Level.SEVERE) {
this.next(<LogEntry>[Level.SEVERE, this.id, Date.now(), ...args]);
}
this.level <= LogLevel.SEVERE &&
super.next(<LogEntry>[
LogLevel.SEVERE,
this.id,
Date.now(),
...args
]);
}
}
9 changes: 5 additions & 4 deletions packages/rstream-log/src/xform/filter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { LogLevel } from "@thi.ng/api";
import { isString } from "@thi.ng/checks";
import { Transducer } from "@thi.ng/transducers";
import { filter } from "@thi.ng/transducers";
import { Level, LogEntry } from "../api";
import { LogEntry } from "../api";

export const onlyLevel = (level: Level): Transducer<LogEntry, LogEntry> =>
export const onlyLevel = (level: LogLevel): Transducer<LogEntry, LogEntry> =>
filter((l) => l[0] === level);

export const minLevel = (level: Level): Transducer<LogEntry, LogEntry> =>
export const minLevel = (level: LogLevel): Transducer<LogEntry, LogEntry> =>
filter((l) => l[0] >= level);

export const maxLevel = (level: Level): Transducer<LogEntry, LogEntry> =>
export const maxLevel = (level: LogLevel): Transducer<LogEntry, LogEntry> =>
filter((l) => l[0] <= level);

export const matchID = (id: string | RegExp): Transducer<LogEntry, LogEntry> =>
Expand Down
19 changes: 14 additions & 5 deletions packages/rstream-log/src/xform/format.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { map, Transducer } from "@thi.ng/transducers";
import { Level, BodyFormat, DateFormat, LogEntry, LogEntryObj } from "../api";
import {
BodyFormat,
DateFormat,
LogEntry,
LogEntryObj
} from "../api";

const LEVELS = ["FINE", "DEBUG", "INFO", "WARN", "SEVERE"];

export const isoDate = (dt: number) => new Date(dt).toISOString();

export const formatString = (
dtFmt?: DateFormat,
bodyFmt?: BodyFormat
): Transducer<LogEntry, string> => {
dtFmt = dtFmt || ((dt) => new Date(dt).toISOString());
dtFmt = dtFmt || isoDate;
bodyFmt = bodyFmt || ((x) => x.toString());
return map(
([level, id, time, ...body]) =>
`[${Level[level]}] [${id}] ${dtFmt(time)} ${bodyFmt(body)}`
`[${LEVELS[level]}] [${id}] ${dtFmt(time)} ${bodyFmt(body)}`
);
};

Expand All @@ -19,11 +28,11 @@ export const formatObject = (): Transducer<LogEntry, LogEntryObj> =>
export const formatJSON = (
dtfmt?: DateFormat
): Transducer<LogEntry, string> => {
dtfmt = dtfmt || ((dt) => new Date(dt).toISOString());
dtfmt = dtfmt || isoDate;
return map(([level, id, time, ...body]) =>
JSON.stringify({
level: Level[level],
id,
level: LEVELS[level],
time: dtfmt(time),
body
})
Expand Down

0 comments on commit ef9bf8d

Please sign in to comment.