Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bogeychan/elysia-logger

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bogeychan/elysia-logger - npm Package Compare versions

Comparing version 0.0.10 to 0.0.11

17

dist/index.d.ts
import Elysia from 'elysia';
import type { LoggerOptions, FileLoggerOptions, StreamLoggerOptions } from './types';
import type { Logger, ElysiaLogger, FileLoggerOptions, StreamLoggerOptions, ElysiaLoggerOptions, StandaloneLoggerOptions } from './types';
export declare const logger: (options?: StreamLoggerOptions) => Elysia<"", {
request: {
log: import("pino").Logger<Omit<LoggerOptions, "customProps">>;
log: Logger;
};

@@ -14,3 +14,3 @@ store: {};

request: {
log: import("pino").Logger<Omit<LoggerOptions, "customProps">>;
log: Logger;
};

@@ -22,4 +22,13 @@ store: {};

}, {}, {}, false>;
export declare function createPinoLogger(options?: Omit<LoggerOptions, 'customProps'>): import("pino").Logger<Omit<LoggerOptions, "customProps">>;
export declare function createPinoLogger(options?: StandaloneLoggerOptions): ElysiaLogger<ReturnType<typeof into>>;
declare function into(this: Logger, options?: ElysiaLoggerOptions): Elysia<"", {
request: {
log: Logger;
};
store: {};
}, {
type: {};
error: {};
}, {}, {}, false>;
export * from './config';
export type { InferContext } from './types';

@@ -7,2 +7,7 @@ import pino from 'pino';

export function createPinoLogger(options = {}) {
const log = createPinoLoggerInternal(options);
log.into = into.bind(log);
return log;
}
function createPinoLoggerInternal(options) {
if (!options.level) {

@@ -24,13 +29,12 @@ options.level = 'info';

}
function plugin(options) {
function into(options = {}) {
return new Elysia({
name: '@bogeychan/elysia-logger'
}).derive((ctx) => {
let log = createPinoLogger(options);
if (typeof options.customProps === 'function') {
log = log.child(options.customProps(ctx));
}
return { log };
});
}).derive((ctx) => ({
log: typeof options.customProps === 'function'
? this.child(options.customProps(ctx))
: this
}));
}
const plugin = (options) => into.bind(createPinoLoggerInternal(options))(options);
export * from './config';

@@ -11,2 +11,7 @@ /// <reference types="bun-types" />

export type LoggerOptions = StreamLoggerOptions | FileLoggerOptions;
export type ElysiaLoggerOptions = Pick<BaseLoggerOptions, 'customProps'>;
export type StandaloneLoggerOptions = Omit<LoggerOptions, 'customProps'>;
export interface ElysiaLogger<E extends Elysia = Elysia> extends Logger {
into(options?: ElysiaLoggerOptions): E;
}
type BaseLoggerOptions = Omit<pino.LoggerOptions, 'level'> & {

@@ -16,4 +21,4 @@ level?: pino.LevelWithSilent | (string & {});

};
export type Logger = pino.Logger;
export type Logger<Options = StandaloneLoggerOptions> = pino.Logger<Options>;
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.10",
"version": "0.0.11",
"description": "A plugin for Elysia.js for logging using the pino library",

@@ -5,0 +5,0 @@ "author": {

@@ -87,25 +87,23 @@ # @bogeychan/elysia-logger

return 'with-context';
})
.listen(8080);
});
```
You can find the entire example in the [examples](./examples/with-context) folder.
### Use the logger instance both `Standalone` and inside `Context`
### Customize the logger name in the request context
```ts
import { createPinoLogger } from '@bogeychan/elysia-logger';
You can learn more about this in the [Elysia's 0.7 blog](https://elysiajs.com/blog/elysia-07.html)
const log = createPinoLogger(/* ... */);
```ts
import { Elysia } from 'elysia';
import { logger } from '@bogeychan/elysia-logger';
const app = new Elysia()
.use(logger().decorate(({ log, ...rest }) => ({ myLogger: log, ...rest })))
app
.use(log.into(/* ... */))
.onError((ctx) => {
log.error(ctx, ctx.error.name);
return 'onError';
})
.get('/', (ctx) => {
// property "myLogger" is available instead of "log"
ctx.myLogger.info(ctx.request, 'Request');
ctx.log.info(ctx, 'Context');
return 'Hello World';
})
.listen(8080);
throw { message: '1234', name: 'MyError' };
});
```

@@ -112,0 +110,0 @@

@@ -5,5 +5,9 @@ import pino from 'pino';

import type {
Logger,
ElysiaLogger,
LoggerOptions,
FileLoggerOptions,
StreamLoggerOptions
StreamLoggerOptions,
ElysiaLoggerOptions,
StandaloneLoggerOptions
} from './types';

@@ -35,4 +39,10 @@

export function createPinoLogger(
options: Omit<LoggerOptions, 'customProps'> = {}
) {
options: StandaloneLoggerOptions = {}
): ElysiaLogger<ReturnType<typeof into>> {
const log = createPinoLoggerInternal(options);
(log as unknown as ElysiaLogger).into = into.bind(log);
return log as unknown as ElysiaLogger<ReturnType<typeof into>>;
}
function createPinoLoggerInternal(options: StandaloneLoggerOptions) {
if (!options.level) {

@@ -53,3 +63,2 @@ options.level = 'info';

if ('file' in options) {
// @ts-ignore options.file
streamOptions.stream = pino.destination(options.file);

@@ -62,17 +71,16 @@ delete (options as Partial<FileLoggerOptions>).file;

function plugin(options: LoggerOptions) {
function into(this: Logger, options: ElysiaLoggerOptions = {}) {
return new Elysia({
name: '@bogeychan/elysia-logger'
}).derive((ctx) => {
let log = createPinoLogger(options);
}).derive((ctx) => ({
log:
typeof options.customProps === 'function'
? this.child(options.customProps(ctx))
: this
}));
}
if (typeof options.customProps === 'function') {
// @ts-ignore
log = log.child(options.customProps(ctx));
}
const plugin = (options: LoggerOptions) =>
into.bind(createPinoLoggerInternal(options))(options);
return { log };
});
}
export * from './config';

@@ -79,0 +87,0 @@

@@ -23,2 +23,26 @@ import type { pino } from 'pino';

export type ElysiaLoggerOptions = Pick<BaseLoggerOptions, 'customProps'>;
export type StandaloneLoggerOptions = Omit<LoggerOptions, 'customProps'>;
export interface ElysiaLogger<E extends Elysia = Elysia> extends Logger {
/**
* Call `into` to use the logger instance in both `ctx` and standalone
*
* @example
* const log = createPinoLogger(...);
* app
* .use(log.into())
* .onError((ctx) => {
* log.error(ctx, ctx.error.name);
* return 'onError';
* })
* .get('/', (ctx) => {
* ctx.log.info(ctx, 'Context');
* throw { message: '1234', name: 'MyError' };
* })
*/
into(options?: ElysiaLoggerOptions): E;
}
type BaseLoggerOptions = Omit<pino.LoggerOptions, 'level'> & {

@@ -44,3 +68,3 @@ /**

export type Logger = pino.Logger;
export type Logger<Options = StandaloneLoggerOptions> = pino.Logger<Options>;

@@ -47,0 +71,0 @@ export type InferContext<T extends Elysia> = T extends Elysia<

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc