Skip to content

Commit

Permalink
Cloudflare workers does not have global environment variables. Instea…
Browse files Browse the repository at this point in the history
…d they are passed into an entrypoint function. These varibales are always available on c.env regardless of environment through. This branch adds the ability to specify LOG_LEVEL as an environment variable and have it respected by the loggers produced in this package
  • Loading branch information
dmaesj committed Nov 16, 2024
1 parent b4de4cb commit c51577a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import type { LiteralString } from "./utils";
export const pinoLogger = <ContextKey extends string = "logger">(
opts?: Options<LiteralString<ContextKey>>,
): MiddlewareHandler<Env<ContextKey>> => {
const rootLogger = isPino(opts?.pino) ? opts.pino : pino(opts?.pino);
const contextKey = opts?.contextKey ?? ("logger" as ContextKey);

return async (c, next) => {
const rootLogger = isPino(opts?.pino) ? opts.pino : pino(opts?.pino);
const contextKey = opts?.contextKey ?? ("logger" as ContextKey);

// use LOG_LEVEL environemnt variable if set
rootLogger.level = c.env.LOG_LEVEL ?? rootLogger.level;

const logger = new PinoLogger(rootLogger);
c.set(contextKey, logger);

Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ export type HttpLoggerOptions = {
* ```
*/
export type Env<LoggerKey extends string = "logger"> = {
Bindings: {
LOG_LEVEL?: string;
};
Variables: {
[key in LoggerKey]: PinoLogger;
};
Expand Down

0 comments on commit c51577a

Please sign in to comment.