Skip to content

Commit

Permalink
feat: added AWS Lambda context to log events
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Jan 10, 2025
1 parent 629a99b commit 6ba4fdf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions logger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 32 additions & 4 deletions logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion logger/deno.jsonc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 6 additions & 4 deletions logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down

0 comments on commit 6ba4fdf

Please sign in to comment.