Skip to content

Commit

Permalink
Make format a const, enforce type check (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
otherguy authored Nov 24, 2023
1 parent f07ef7e commit f106460
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 28 deletions.
4 changes: 2 additions & 2 deletions examples/basic.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";

// Use console for logging
const logger : Logger = console;

// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
// Log in short format
format: "short",
format: LogFormat.SHORT
});

// Create Elysia app
Expand Down
6 changes: 3 additions & 3 deletions examples/bunyan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";
import { createLogger, INFO } from 'bunyan'

// Define Bunyan logger
Expand All @@ -11,8 +11,8 @@ const logger : Logger = createLogger ({

const elysiaLogging = ElysiaLogging(logger, {
// Access logs in JSON format
format: "json",
})
format: LogFormat.JSON
});

//
const app = new Elysia()
Expand Down
4 changes: 2 additions & 2 deletions examples/json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";

// Use console for logging
const logger : Logger = console;

// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
format: "json",
format: LogFormat.JSON
});

// Create Elysia app
Expand Down
4 changes: 2 additions & 2 deletions examples/on-error.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";

// Use console for logging
const logger : Logger = console;

// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
format: "short",
format: LogFormat.SHORT
});

// Create Elysia app
Expand Down
4 changes: 2 additions & 2 deletions examples/pino-pretty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";
import { pino } from "pino";

// Define Pino logger
Expand Down Expand Up @@ -52,7 +52,7 @@ const elysiaLogging = ElysiaLogging(logger, {
level: "http",

// Access logs in JSON format
format: "json"
format: LogFormat.JSON
});

//
Expand Down
6 changes: 3 additions & 3 deletions examples/pino.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";
import { pino, type Logger as PinoLogger, LoggerOptions } from "pino";

// Define a custom Pino logger interface that includes the "http" level
Expand Down Expand Up @@ -40,8 +40,8 @@ const elysiaLogging = ElysiaLogging(logger as Logger, {
level: "http",

// Access logs in JSON format
format: "json",
})
format: LogFormat.JSON,
});

//
const app = new Elysia()
Expand Down
7 changes: 3 additions & 4 deletions examples/tslog.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";
import { Logger as TSLog, ILogObj } from "tslog";

// Define TSLog logger
const logger : TSLog<ILogObj> = new TSLog();

// Define a custom TSLog logger interface that includes the "http" level
const elysiaLogging = ElysiaLogging(logger as Logger, {
level: "info",
format: "short",
})
format: LogFormat.SHORT,
});

//
const app = new Elysia()
Expand Down
4 changes: 2 additions & 2 deletions examples/winston.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { type Logger, LogFormat } from "../src/types";
import { createLogger, transports, format } from "winston";

// Define Winston logger
Expand All @@ -20,7 +20,7 @@ const elysiaLogging = ElysiaLogging(logger, {
level: "http",

// Access logs in JSON format
format: "json",
format: LogFormat.JSON,
})

//
Expand Down
9 changes: 2 additions & 7 deletions spec/elysiaLogging.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger } from "../src/types";
import { LogFormatType, type Logger } from "../src/types";

interface MockLogger extends Logger {
debug: <T extends unknown[]>(...args: T) => void;
info: <T extends unknown[]>(...args: T) => void;
warn: <T extends unknown[]>(...args: T) => void;
error: <T extends unknown[]>(...args: T) => void;
http: <T extends unknown[]>(...args: T) => void;
}


describe("ElysiaLogging", () => {
let app: Elysia;
let logger: MockLogger;
Expand Down Expand Up @@ -169,7 +164,7 @@ describe("ElysiaLogging", () => {

it("throws an error if format is not a valid format string", () => {
const middleware = ElysiaLogging(logger, {
format: "invalid-format"
format: "invalid-format" as unknown as LogFormatType
});

expect(() => app.use(middleware)).toThrow("Formatter 'invalid-format' not found!");
Expand Down
1 change: 1 addition & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { formatDuration, parseBasicAuthHeader } from './helpers';
* console.log(log.formatJson());
**/
export class Log {

// Properties
private logObject: LogObject;

Expand Down
39 changes: 38 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import type { Context } from "elysia";
import { Log } from "./log";

// This creates a type that is like "json" | "common" | "short"
type LogFormatString = {[K in keyof typeof Log.prototype as K extends `format${infer Rest}` ? Lowercase<Rest> : never]: typeof Log.prototype[K]};

// This is the type of a function that takes a LogObject and returns a string or a LogObject
type LogFormatMethod = (log: LogObject) => string | LogObject;

// This creates a LogFormat const that is like "JSON"="json", "COMMON"="common", "SHORT"="short"
type LogFormatRecord = Record<Uppercase<keyof LogFormatString>, string>;

//
export const LogFormat = {
JSON: 'json',
COMMON: 'common',
SHORT: 'short',
// Add other methods here
} as const;

export type LogFormatType = keyof LogFormatString | LogFormatMethod | LogFormatter | LogFormatRecord;

/**
* Represents the basic authentication credentials.
Expand Down Expand Up @@ -70,7 +90,7 @@ export type LogObject = {
*/
export interface RequestLoggerOptions {
level?: string;
format?: string | ((log: LogObject) => string | LogObject);
format?: LogFormatType, // string | ((log: LogObject) => string | LogObject);
includeHeaders?: string[];
skip?: (ctx: Context) => boolean;
ipHeaders?: IPHeaders[];
Expand All @@ -85,3 +105,20 @@ export interface Logger {
warn: <T extends unknown[]>(...args: T) => void;
error: <T extends unknown[]>(...args: T) => void;
}

/**
* Interface for a log formatter.
*
* A log formatter is a class with a format() method that takes a log
* object and returns a string or a log object.
*/
export interface LogFormatter {
/**
* Formats a log object.
*
* @param log Log object to format
*
* @returns Formatted log object or string
*/
format(log: LogObject): string | LogObject;
}
3 changes: 3 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
"esModuleInterop": true,
"allowJs": true,
},
"include": [
"src/**/*"
],
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
/* Include */
"include": [
"src/**/*",
"spec/**/*",
"examples/**/*"
],
}

0 comments on commit f106460

Please sign in to comment.