@bogeychan/elysia-logger
Advanced tools
Comparing version 0.0.9 to 0.0.10
export declare const formatters: { | ||
log(object: Record<string, unknown>): Record<string, unknown> | { | ||
log(object: Record<string, unknown>): { | ||
method: string; | ||
url: string; | ||
referrer: string; | ||
}; | ||
} | Record<string, unknown>; | ||
}; | ||
export declare function isContext(object: unknown): {}; | ||
export declare function isRequest(object: unknown): string; |
import Elysia from 'elysia'; | ||
import type { LoggerOptions, FileLoggerOptions, StreamLoggerOptions } from './types'; | ||
export declare const logger: <ContextKeyName extends string = "log">(options?: StreamLoggerOptions<ContextKeyName>) => Elysia<"", { | ||
export declare const logger: (options?: StreamLoggerOptions) => Elysia<"", { | ||
request: { | ||
log: import("pino").Logger<Omit<LoggerOptions, "customProps">>; | ||
}; | ||
store: {}; | ||
}, { | ||
type: {}; | ||
error: {}; | ||
request: {} & Awaited<{ [K in ContextKeyName]: import("pino").Logger<Omit<LoggerOptions<ContextKeyName>, "customProps" | "contextKeyName">>; }>; | ||
schema: {}; | ||
meta: { | ||
schema: {}; | ||
defs: {}; | ||
exposed: {}; | ||
}, {}, {}, false>; | ||
export declare const fileLogger: (options: FileLoggerOptions) => Elysia<"", { | ||
request: { | ||
log: import("pino").Logger<Omit<LoggerOptions, "customProps">>; | ||
}; | ||
}>; | ||
export declare const fileLogger: <ContextKeyName extends string = "log">(options: FileLoggerOptions<ContextKeyName>) => Elysia<"", { | ||
store: {}; | ||
}, { | ||
type: {}; | ||
error: {}; | ||
request: {} & Awaited<{ [K in ContextKeyName]: import("pino").Logger<Omit<LoggerOptions<ContextKeyName>, "customProps" | "contextKeyName">>; }>; | ||
schema: {}; | ||
meta: { | ||
schema: {}; | ||
defs: {}; | ||
exposed: {}; | ||
}; | ||
}>; | ||
export declare function createPinoLogger<ContextKeyName extends string = string>(options?: Omit<LoggerOptions<ContextKeyName>, 'customProps' | 'contextKeyName'>): import("pino").Logger<Omit<LoggerOptions<ContextKeyName>, "customProps" | "contextKeyName">>; | ||
}, {}, {}, false>; | ||
export declare function createPinoLogger(options?: Omit<LoggerOptions, 'customProps'>): import("pino").Logger<Omit<LoggerOptions, "customProps">>; | ||
export * from './config'; | ||
export type { ElysiaContextForInstance, InferElysiaInstance } from './types'; | ||
export type { InferContext } from './types'; |
@@ -24,18 +24,12 @@ import pino from 'pino'; | ||
function plugin(options) { | ||
if (!options.contextKeyName) { | ||
options.contextKeyName = 'log'; | ||
} | ||
const { contextKeyName, ...loggerOptions } = options; | ||
return new Elysia({ | ||
name: '@bogeychan/elysia-logger' | ||
}).derive((ctx) => { | ||
let log = createPinoLogger(loggerOptions); | ||
let log = createPinoLogger(options); | ||
if (typeof options.customProps === 'function') { | ||
log = log.child(options.customProps(ctx)); | ||
} | ||
return { | ||
[contextKeyName]: log | ||
}; | ||
return { log }; | ||
}); | ||
} | ||
export * from './config'; |
/// <reference types="bun-types" /> | ||
import type { pino } from 'pino'; | ||
import type { Context, Elysia, ElysiaInstance } from 'elysia'; | ||
export type StreamLoggerOptions<ContextKeyName extends string> = BaseLoggerOptions<ContextKeyName> & { | ||
import type { Context, DecoratorBase, Elysia } from 'elysia'; | ||
export type StreamLoggerOptions = BaseLoggerOptions & { | ||
stream?: pino.DestinationStream; | ||
}; | ||
export type FileLoggerOptions<ContextKeyName extends string> = BaseLoggerOptions<ContextKeyName> & { | ||
export type FileLoggerOptions = BaseLoggerOptions & { | ||
file: PathLike; | ||
}; | ||
export type LoggerOptions<ContextKeyName extends string> = StreamLoggerOptions<ContextKeyName> | FileLoggerOptions<ContextKeyName>; | ||
type BaseLoggerOptions<ContextKeyName extends string> = Omit<pino.LoggerOptions, 'level'> & { | ||
export type LoggerOptions = StreamLoggerOptions | FileLoggerOptions; | ||
type BaseLoggerOptions = Omit<pino.LoggerOptions, 'level'> & { | ||
level?: pino.LevelWithSilent | (string & {}); | ||
contextKeyName?: ContextKeyName; | ||
customProps?: <Instance extends ElysiaInstance>(ctx: ElysiaContextForInstance<Instance>) => object; | ||
customProps?: <Instance extends Elysia>(ctx: InferContext<Instance>) => object; | ||
}; | ||
export type Logger = pino.Logger; | ||
export type InferElysiaInstance<T> = T extends Elysia<infer _BasePath, infer U> ? U : never; | ||
export type ElysiaContextForInstance<Instance extends ElysiaInstance> = Context<Instance['schema'], Instance['store']> & Partial<Instance['request']>; | ||
export type InferContext<T extends Elysia> = T extends Elysia<infer Path, infer Decorators, infer _Definitions, infer _ParentSchema, infer Routes> ? Context<Routes, DecoratorBase, Path> & Partial<Decorators['request']> : never; | ||
export {}; |
{ | ||
"name": "@bogeychan/elysia-logger", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "A plugin for Elysia.js for logging using the pino library", | ||
@@ -36,7 +36,7 @@ "author": { | ||
"peerDependencies": { | ||
"elysia": ">= 0.6.19" | ||
"elysia": ">= 0.7.6" | ||
}, | ||
"devDependencies": { | ||
"bun-types": "^1.0.1", | ||
"elysia": "^0.6.19", | ||
"bun-types": "^1.0.2", | ||
"elysia": "^0.7.6", | ||
"typescript": "^5.2.2" | ||
@@ -43,0 +43,0 @@ }, |
# @bogeychan/elysia-logger | ||
A plugin for [Elysia.js](https://elysiajs.com) for logging using the [pino](https://getpino.io) library | ||
A plugin for [Elysia.js](https://elysiajs.com) for logging using the [pino](https://getpino.io) library. | ||
## [Migration guide](./MIGRATION.md) | ||
## Installation | ||
@@ -57,3 +59,3 @@ | ||
```ts | ||
import { logger } from '@bogeychan/elysia-logger'; | ||
import { logger, type InferContext } from '@bogeychan/elysia-logger'; | ||
@@ -73,5 +75,3 @@ const myPlugin = () => (app: Elysia) => app.decorate('myProperty', 42); | ||
*/ | ||
customProps( | ||
ctx: ElysiaContextForInstance<InferElysiaInstance<typeof app>> | ||
) { | ||
customProps(ctx: InferContext<typeof app>) { | ||
return { | ||
@@ -97,2 +97,4 @@ params: ctx.params, | ||
You can learn more about this in the [Elysia's 0.7 blog](https://elysiajs.com/blog/elysia-07.html) | ||
```ts | ||
@@ -103,7 +105,3 @@ import { Elysia } from 'elysia'; | ||
const app = new Elysia() | ||
.use( | ||
logger({ | ||
contextKeyName: 'myLogger' | ||
}) | ||
) | ||
.use(logger().decorate(({ log, ...rest }) => ({ myLogger: log, ...rest }))) | ||
.get('/', (ctx) => { | ||
@@ -110,0 +108,0 @@ // property "myLogger" is available instead of "log" |
@@ -23,5 +23,3 @@ import pino from 'pino'; | ||
*/ | ||
export const logger = <ContextKeyName extends string = 'log'>( | ||
options: StreamLoggerOptions<ContextKeyName> = {} | ||
) => plugin(options); | ||
export const logger = (options: StreamLoggerOptions = {}) => plugin(options); | ||
@@ -31,5 +29,3 @@ /** | ||
*/ | ||
export const fileLogger = <ContextKeyName extends string = 'log'>( | ||
options: FileLoggerOptions<ContextKeyName> | ||
) => plugin(options); | ||
export const fileLogger = (options: FileLoggerOptions) => plugin(options); | ||
@@ -39,7 +35,4 @@ /** | ||
*/ | ||
export function createPinoLogger<ContextKeyName extends string = string>( | ||
options: Omit< | ||
LoggerOptions<ContextKeyName>, | ||
'customProps' | 'contextKeyName' | ||
> = {} | ||
export function createPinoLogger( | ||
options: Omit<LoggerOptions, 'customProps'> = {} | ||
) { | ||
@@ -58,3 +51,3 @@ if (!options.level) { | ||
const streamOptions = options as StreamLoggerOptions<ContextKeyName>; | ||
const streamOptions = options as StreamLoggerOptions; | ||
@@ -64,3 +57,3 @@ if ('file' in options) { | ||
streamOptions.stream = pino.destination(options.file); | ||
delete (options as Partial<FileLoggerOptions<ContextKeyName>>).file; | ||
delete (options as Partial<FileLoggerOptions>).file; | ||
} | ||
@@ -71,32 +64,15 @@ | ||
function plugin<ContextKeyName extends string>( | ||
options: LoggerOptions<ContextKeyName> | ||
) { | ||
if (!options.contextKeyName) { | ||
options.contextKeyName = 'log' as ContextKeyName; | ||
} | ||
const { contextKeyName, ...loggerOptions } = options; | ||
type DeriveReturned = { | ||
[K in ContextKeyName]: ReturnType<typeof createPinoLogger<ContextKeyName>>; | ||
}; | ||
function plugin(options: LoggerOptions) { | ||
return new Elysia({ | ||
name: '@bogeychan/elysia-logger' | ||
}).derive<DeriveReturned>( | ||
// @ts-ignore | ||
(ctx) => { | ||
let log = createPinoLogger(loggerOptions); | ||
}).derive((ctx) => { | ||
let log = createPinoLogger(options); | ||
if (typeof options.customProps === 'function') { | ||
// @ts-ignore | ||
log = log.child(options.customProps(ctx)); | ||
} | ||
if (typeof options.customProps === 'function') { | ||
// @ts-ignore | ||
log = log.child(options.customProps(ctx)); | ||
} | ||
return { | ||
[contextKeyName]: log | ||
}; | ||
} | ||
); | ||
return { log }; | ||
}); | ||
} | ||
@@ -106,3 +82,3 @@ | ||
export type { ElysiaContextForInstance, InferElysiaInstance } from './types'; | ||
export type { InferContext } from './types'; | ||
import type { pino } from 'pino'; | ||
import type { Context, Elysia, ElysiaInstance } from 'elysia'; | ||
import type { Context, DecoratorBase, Elysia } from 'elysia'; | ||
@@ -7,6 +7,5 @@ /** | ||
*/ | ||
export type StreamLoggerOptions<ContextKeyName extends string> = | ||
BaseLoggerOptions<ContextKeyName> & { | ||
stream?: pino.DestinationStream; | ||
}; | ||
export type StreamLoggerOptions = BaseLoggerOptions & { | ||
stream?: pino.DestinationStream; | ||
}; | ||
@@ -16,6 +15,5 @@ /** | ||
*/ | ||
export type FileLoggerOptions<ContextKeyName extends string> = | ||
BaseLoggerOptions<ContextKeyName> & { | ||
file: PathLike; | ||
}; | ||
export type FileLoggerOptions = BaseLoggerOptions & { | ||
file: PathLike; | ||
}; | ||
@@ -25,10 +23,5 @@ /** | ||
*/ | ||
export type LoggerOptions<ContextKeyName extends string> = | ||
| StreamLoggerOptions<ContextKeyName> | ||
| FileLoggerOptions<ContextKeyName>; | ||
export type LoggerOptions = StreamLoggerOptions | FileLoggerOptions; | ||
type BaseLoggerOptions<ContextKeyName extends string> = Omit< | ||
pino.LoggerOptions, | ||
'level' | ||
> & { | ||
type BaseLoggerOptions = Omit<pino.LoggerOptions, 'level'> & { | ||
/** | ||
@@ -45,20 +38,7 @@ * One of the supported levels or `silent` to disable logging. | ||
/** | ||
* Customize the logger name in the request context | ||
* | ||
* @example | ||
* const app = new Elysia() | ||
* .use(logger({ contextKeyName: 'myLogger' })) | ||
* .get('/', (ctx) => { | ||
* // property "myLogger" is available instead of "log" | ||
* ctx.myLogger.info(ctx.request, 'Request'); | ||
* // ... | ||
* }).listen(8080); | ||
*/ | ||
contextKeyName?: ContextKeyName; | ||
/** | ||
* This function will be invoked for each `log`-method called with `context` | ||
* where you can pass additional properties that need to be logged | ||
*/ | ||
customProps?: <Instance extends ElysiaInstance>( | ||
ctx: ElysiaContextForInstance<Instance> | ||
customProps?: <Instance extends Elysia>( | ||
ctx: InferContext<Instance> | ||
) => object; | ||
@@ -69,11 +49,11 @@ }; | ||
export type InferElysiaInstance<T> = T extends Elysia<infer _BasePath, infer U> | ||
? U | ||
export type InferContext<T extends Elysia> = T extends Elysia< | ||
infer Path, | ||
infer Decorators, | ||
infer _Definitions, | ||
infer _ParentSchema, | ||
infer Routes | ||
> | ||
? Context<Routes, DecoratorBase, Path> & Partial<Decorators['request']> | ||
: never; | ||
export type ElysiaContextForInstance<Instance extends ElysiaInstance> = Context< | ||
Instance['schema'], | ||
Instance['store'] | ||
> & | ||
Partial<Instance['request']>; | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19
15027
273
122