Skip to content

Commit

Permalink
cleaner Logger API and types
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro committed Dec 14, 2022
1 parent 908d244 commit 4c5a909
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
54 changes: 28 additions & 26 deletions src/common/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ import { LoggerFormat } from "../../server/models/server-settings/server-setting
import { noop, Unary } from "../utils/functional/functional";
import { isoNow } from "../utils/time/time";

type LogFn = (msg: string, extra?: Record<string, string>) => void;

export interface Logger {
log: Function;
warn: Function;
error: Function;
addPrefix: Unary<String, Logger>;
log: LogFn;
warn: LogFn;
error: LogFn;
setLoggerId: Unary<String, Logger>;
}

class JSONLogger implements Logger {

constructor(private logger = "turnilo") {
}

log(message: string, extra: Record<string, string>) {
log(message: string, extra: Record<string, string> = {}) {
console.log(JSON.stringify({
message,
time: isoNow(),
Expand All @@ -40,11 +42,11 @@ class JSONLogger implements Logger {
}));
}

addPrefix(prefix: string): Logger {
return new JSONLogger(prefix);
setLoggerId(loggerId: string): Logger {
return new JSONLogger(loggerId);
}

error(message: string, extra: Record<string, string>) {
error(message: string, extra: Record<string, string> = {}) {
console.log(JSON.stringify({
message,
time: isoNow(),
Expand All @@ -54,7 +56,7 @@ class JSONLogger implements Logger {
}));
}

warn(message: string, extra: Record<string, string>) {
warn(message: string, extra: Record<string, string> = {}) {
console.log(JSON.stringify({
message,
time: isoNow(),
Expand All @@ -66,49 +68,49 @@ class JSONLogger implements Logger {
}

class ConsoleLogger implements Logger {
constructor(private prefixes: string[] = []) {
constructor(private prefix = "") {
}

error(...args: any[]) {
console.error(...this.prefixes, ...args);
error(message: string) {
console.error(this.prefix, message);
}

warn(...args: any[]) {
console.warn(...this.prefixes, ...args);
warn(message: string) {
console.warn(this.prefix, message);
}

log(...args: any[]) {
console.log(...this.prefixes, ...args);
log(message: string) {
console.log(this.prefix, message);
}

addPrefix(prefix: string): Logger {
return new ConsoleLogger([...this.prefixes, prefix]);
setLoggerId(loggerId: string): Logger {
return new ConsoleLogger(loggerId);
}
}

class ErrorLogger implements Logger {
addPrefix(): Logger {
setLoggerId(): Logger {
return this;
}

error(...args: any[]) {
console.error(...args);
error(message: string) {
console.error(message);
}

log(...args: any[]) {
console.error(...args);
log(message: string) {
console.error(message);
}

warn(...args: any[]) {
console.error(...args);
warn(message: string) {
console.error(message);
}
}

export const NOOP_LOGGER: Logger = {
error: noop,
warn: noop,
log: noop,
addPrefix: noop
setLoggerId: () => NOOP_LOGGER
};

const LOGGERS: Record<LoggerFormat, Logger> = {
Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/time-monitor/time-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class TimeMonitor {
private doingChecks = false;

constructor(logger: Logger) {
this.logger = logger.addPrefix("TimeMonitor");
this.logger = logger.setLoggerId("TimeMonitor");
this.checks = new Map();
this.timekeeper = Timekeeper.EMPTY;
setInterval(this.doChecks, 1000);
Expand Down Expand Up @@ -58,7 +58,7 @@ export class TimeMonitor {
logger.log(`Got the latest time for '${name}' (${updatedTime.toISOString()})`);
this.timekeeper = this.timekeeper.updateTime(name, updatedTime);
}).catch(e => {
logger.error(`Failed getting time for '${name}', using previous time.`, `Error: ${e.message}`);
logger.error(`Failed getting time for '${name}', using previous time. Error: ${e.message}`);
this.timekeeper = this.timekeeper.updateTime(name, previousTime);
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default function createApp(serverSettings: ServerSettings, settingsManage
serverSettings,
settingsManager.appSettings,
settingsManager.sourcesGetter,
settingsManager.logger.addPrefix(name));
settingsManager.logger.setLoggerId(name));
} catch (e) {
settingsManager.logger.warn(`Plugin ${name} threw an error: ${e.message}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/sources/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { SettingsManager } from "../../utils/settings-manager/settings-manager";

export function sourcesRouter(settings: Pick<SettingsManager, "getSources" | "logger">) {

const logger = settings.logger.addPrefix("Settings Endpoint: ");
const logger = settings.logger.setLoggerId("Sources");

const router = Router();

Expand Down
2 changes: 1 addition & 1 deletion src/server/utils/cluster-manager/cluster-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class ClusterManager {
}

logger.log(`Cluster ${cluster.name} creating requestDecorator`);
return module.druidRequestDecoratorFactory(logger.addPrefix("DruidRequestDecoratorFactory"), {
return module.druidRequestDecoratorFactory(logger.setLoggerId("DruidRequestDecoratorFactory"), {
options: cluster.requestDecorator.options,
cluster
});
Expand Down

0 comments on commit 4c5a909

Please sign in to comment.