Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added AWS Lambda context to log events #19

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 36 additions & 5 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,10 +28,41 @@ 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`)
- from env `K_REVISION` - set by Knative such as Google Cloud Run
- from env`AWS_LAMBDA_FUNCTION_NAME` - set by AWS Lambda
- from `os.hostname()` - fallback
- `serviceName` - the service name (e.g. `my-service`)
- from env `SERVICE_NAME` - set by your deployment
- from env `K_SERVICE` - set by Knative such as Google Cloud Run
- `stage` - the stage (e.g. `dev`)
- from env `STAGE` - set by your deployment
- from env `NODE_ENV` - set by your deployment
- `version` - the version (e.g. `1.0.0`)
- from env `SERVICE_VERSION` - set by your deployment
- from env `npm_package_version` - set by your deployment in `package.json`
- `region` - the region (e.g. `eu-west-1`)
- from env `REGION` - set by your deployment
- from env `AWS_REGION` - set by AWS Lambda
- `runtime` - the runtime (e.g. `nodejs-20.11.0`)
- from `process.versions.bun` - set by Bun
- from `process.versions.deno` - set by Deno
- from env `AWS_EXECUTION_ENV` - set by AWS Lambda
- from `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
[syslog log levels](https://github.com/winstonjs/winston?tab=readme-ov-file#logging-levels):
[syslog log levels](https://github.com/winstonjs/winston?tab=readme-ov-file#logging-levels) from most to least severe:

```ts
{
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
12 changes: 7 additions & 5 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.version = process.env.npm_package_version
event.host = process.env.K_REVISION || process.env.AWS_LAMBDA_FUNCTION_NAME || hostName
event.serviceName = process.env.SERVICE_NAME || process.env.K_SERVICE || null
event.stage = process.env.STAGE || process.env.NODE_ENV || null
event.version = process.env.SERVICE_VERSION || process.env.npm_package_version
event.region = process.env.REGION || process.env.AWS_REGION || null
event.runtime = detectProcessVersion()
return event
})
Expand Down
Loading