From 6ba4fdfa054990722b9f45f1c7b063142a3c6aa4 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Fri, 10 Jan 2025 10:30:24 +0100 Subject: [PATCH] feat: added AWS Lambda context to log events --- README.md | 7 +++++-- logger/CHANGELOG.md | 4 ++++ logger/README.md | 36 ++++++++++++++++++++++++++++++++---- logger/deno.jsonc | 2 +- logger/logger.ts | 10 ++++++---- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8c8e3d9..e9e0b3a 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ A collection of utilities for TypeScript and JavaScript. This repository is work in progress. +It is tested against Bun, Deno, and Node.js runtimes. Published on [jsr.io (`@frytg`)](https://jsr.io/@frytg). + ## Tools - [`@frytg/check-required-env`](./check-required-env/README.md) - Check a required environment variable @@ -25,9 +27,10 @@ Other tools that I regularly use and don't feel the need to optimize or re-creat - [`axios`](https://github.com/axios/axios) - _Promise based HTTP client for the browser and node.js_ - [`hono`](https://jsr.io/@hono/hono) - _small, simple, and ultrafast web framework built on Web Standards_ -- [`undici`](https://github.com/nodejs/undici) - performant HTTP/1.1 client for Node.js +- [`undici`](https://github.com/nodejs/undici) - _performant HTTP/1.1 client for Node.js_ - [`@upstash/redis`](https://github.com/upstash/redis-js) for HTTP redis and [`redis`](https://github.com/redis/node-redis) for TCP -- [`@turf/turf`](https://github.com/Turfjs/turf) for anything geospatial +- [`tailwindcss`](https://tailwindcss.com/docs/installation) - _CSS framework for rapid UI development_ +- [`@turf/turf`](https://github.com/Turfjs/turf) _for anything geospatial_ ## Lint diff --git a/logger/CHANGELOG.md b/logger/CHANGELOG.md index 1d2db63..c6e5a99 100644 --- a/logger/CHANGELOG.md +++ b/logger/CHANGELOG.md @@ -1,5 +1,9 @@ # Logger Changelog +## 2025-01-10 - 0.0.3 + +- feat: added AWS Lambda context to log events + ## 2024-11-22 - 0.0.2 - feat: added basic setup diff --git a/logger/README.md b/logger/README.md index 92539e8..1fed402 100644 --- a/logger/README.md +++ b/logger/README.md @@ -3,13 +3,13 @@ [![JSR @frytg/logger](https://jsr.io/badges/@frytg/logger)](https://jsr.io/@frytg/logger) [![ci](https://github.com/frytg/utility/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/frytg/utility/actions/workflows/test.yml) -This is a simple opinionated wrapper around the logging library [Winston](https://github.com/winstonjs/winston) to provide a logger -that is easy to use and configure. +This is a simple opinionated wrapper around the logging library [Winston](https://github.com/winstonjs/winston) +to provide a logger that is easy to use and configure. It accesses and inject several env variables (like runtime or package version) to each log event. -If `IS_LOCAL` is set to `true`, it will use a more human readable, colorized output format, otherwise it will use JSON -on one line. +If `IS_LOCAL` is set to `true`, it will use a more human readable, colorized output format, +otherwise it will use JSON on one line. Debug logs will only be logged if the env `STAGE` is set to `dev`. @@ -28,6 +28,34 @@ logger.log({ }); ``` +## Configuration + +The logger accesses and injects several env variables to each log event (envs listed in order of priority): + +- `host` - the host name (e.g. `my-function`) + - `K_REVISION` - set by Knative such as Google Cloud Run + - `AWS_LAMBDA_FUNCTION_NAME` - set by AWS Lambda + - `os.hostname()` - fallback +- `serviceName` - the service name (e.g. `my-service`) + - `SERVICE_NAME` - set by your deployment +- `stage` - the stage (e.g. `dev`) + - `STAGE` - set by your deployment +- `version` - the version (e.g. `1.0.0`) + - `npm_package_version` - set by your deployment in `package.json` +- `region` - the region (e.g. `eu-west-1`) + - `REGION` - set by your deployment + - `AWS_REGION` - set by AWS Lambda +- `runtime` - the runtime (e.g. `nodejs-20.11.0`) + - `process.versions.bun` - set by Bun + - `process.versions.deno` - set by Deno + - `process.env.AWS_EXECUTION_ENV` - set by AWS Lambda + - `process.version` - fallback (usually Node.js) + +Additionally these environment variables are triggering different logging formats: + +- `IS_LOCAL` - set to `true` to use a more human readable, colorized output format that uses multiple lines +- `STAGE` - set to `dev` to enable debug logs + ## Log Levels It is currently pre-configured with the diff --git a/logger/deno.jsonc b/logger/deno.jsonc index 04ea039..cb70812 100644 --- a/logger/deno.jsonc +++ b/logger/deno.jsonc @@ -1,7 +1,7 @@ { "$schema": "https://jsr.io/schema/config-file.v1.json", "name": "@frytg/logger", - "version": "0.0.2", + "version": "0.0.3", "exports": "./logger.ts", "imports": { "winston": "npm:winston@^3.17.0" diff --git a/logger/logger.ts b/logger/logger.ts index 7c4bffc..d104995 100644 --- a/logger/logger.ts +++ b/logger/logger.ts @@ -32,15 +32,17 @@ const convertError = format((event) => { export const detectProcessVersion = (): string => { if (process.versions?.bun) return `bun-${process.versions.bun}` if (process.versions?.deno) return `deno-${process.versions.deno}` - return process.version + if (process.env.AWS_EXECUTION_ENV) return process.env.AWS_EXECUTION_ENV.replace(/_/g, '-') + return `nodejs-${process.version}` } // Add global context to log events const convertGlobals = format((event) => { - event.host = process.env.K_REVISION || hostName - event.serviceName = process.env.SERVICE_NAME - event.stage = process.env.STAGE + event.host = process.env.K_REVISION || process.env.AWS_LAMBDA_FUNCTION_NAME || hostName + event.serviceName = process.env.SERVICE_NAME || null + event.stage = process.env.STAGE || null event.version = process.env.npm_package_version + event.region = process.env.REGION || process.env.AWS_REGION || null event.runtime = detectProcessVersion() return event })