The JSON logger you always wanted for Lambda.

Overview

MikroLog

The JSON logger you always wanted for Lambda.

MikroLog is like serverless: There is still a logger ("server"), but you get to think a lot less about it and you get the full "It Just Works"™ experience.

Build Status

FOSSA Status

Quality Gate Status

codecov

Maintainability


Loggers have become too opinionated, bloated and complicated. MikroLog provides an option that is:

  • Adapted out-of-the box for serverless Lambda environments (no requirement though!)
  • Gives you multi-level, clean and structured logs
  • Easiest to grok logger that isn't pure console.log()
  • Familiar syntax using log(), info(), debug(), warn() and error()
  • Zero config and opinionated enough to still be awesome without any magic tricks
  • Cuts out all the stuff you won't need in cloud/serverless like storing logs or creating file output
  • None of the pid and other garbage fields you get from many other solutions
  • Flexible for most needs by loading your own static metadata that gets used in all logs
  • Outside of AWS itself, logs carry across perfectly to observability solutions like Datadog, New Relic, Honeycomb...
  • Easy to redact or mask sensitive data
  • Uses process.stdout.write() rather than console.log() so you can safely use it in Lambda
  • Tiny (1.7 KB gzipped)
  • Has zero dependencies
  • Has 100% test coverage

Behavior

MikroLog will throw away any fields that are undefined, null or empty.

You may pass either strings or objects into each logging method. Messages will show up in the message field.

MikroLog accepts certain static metadata from you (user input) and will infer dynamic metadata if you are in an AWS Lambda environment. See more in the Metadata section.

Usage

Basic importing and usage

// ES5 format
const { MikroLog } = require('mikrolog');
// ES6 format
import { MikroLog } from 'mikrolog';

const logger = new MikroLog();

// String message
logger.log('Hello World!');

// Object message
logger.log({
  Hello: 'World!',
  statement: 'Objects work just as well!'
});

Configuration

You may also optionally instantiate MikroLog using a custom metadata object:

const metadata = { service: 'MyService' };
const logger = new MikroLog({ metadataConfig: metadata });

To use the full set of features, including deriving dynamic metadata from AWS Lambda, you would add the event and context objects like so:

// Your Lambda handler doing whatever it does
export async function handler(event: any, context: any) {
  // {...}
  const metadata = { service: 'MyService' };
  const logger = new MikroLog({ metadataConfig: metadata, event, context });
  // {...}
}

See more in the Metadata section.

Logging

Informational logs

Output an informational-level log:

logger.log('My message!');

This log type is also aliased under the info() method if you prefer that syntax:

logger.info('My message!');

Warning logs

Output a warning-level log:

logger.warn('My message!');

Error logs

Output an error-level log:

logger.error('My message!');

Debug logs

Output a debug log:

logger.debug('My message!');

Metadata

Static metadata

This is the metadata that you may provide at the time of instantiation. These fields will then be used automatically in all subsequent logs.

Under the hood, MikroLog is built and tested around practically the same metadata format as seen in catalogist which might look like:

const metadataConfig = {
  version: 1,
  lifecycleStage: 'production',
  owner: 'MyCompany',
  hostPlatform: 'aws',
  domain: 'CustomerAcquisition',
  system: 'ShowroomActivities',
  service: 'UserSignUp',
  team: 'MyDemoTeam',
  tags: ['typescript', 'backend'],
  dataSensitivity: 'public'
};

const logger = new MikroLog({ metadataConfig });

However, you are free to use whatever static metadata you want.

Ideally you store this static metadata configuration in its own file and have unique ones for each service.

Dynamic metadata

The dynamic metadata fields are picked up automatically if you pass them in during instantiation. Most of those metadata fields will relate to unique value types available in AWS Lambda.

If these values are not available, they will be dropped at the time of log output. In effect, this means you won't have to deal with them (being empty or otherwise) if you use MikroLog in another type of context.

/**
 * @description ID of the log.
 */
id: string;
/**
 * @description Timestamp of this message in Unix epoch.
 */
timestamp: string;
/**
 * @description Timestamp of this message in ISO 8601 format.
 */
timestampHuman: string;
/**
 * @description Correlation ID for this function call.
 */
correlationId: string;
/**
 * @description The user in this log context.
 */
user: string;
/**
 * @description The route that is responding. In EventBridge, this will be your detail type.
 * @example `/doSomething`
 */
route: string;
/**
 * @description The region of the responding function/system.
 */
region: string;
/**
 * @description What runtime is used?
 */
runtime: string;
/**
 * @description The name of the funciton.
 */
functionName: string;
/**
 * @description Memory size of the current function.
 */
functionMemorySize: string;
/**
 * @description The version of the function.
 */
functionVersion: string;
/**
 * @description What AWS stage are we in?
 */
stage: string;
/**
 * @description The AWS account ID that the system is running in.
 */
accountId: string;
/**
 * @description Request time in Unix epoch of the incoming request.
 */
requestTimeEpoch: string;

Redacting keys or masking values

In your static metadata you can add some extra security measures with two different string arrays:

  • redactedKeys: Any items in this array will be completely removed from log output.
  • maskedValues: Any items in this array will have MASKED as their value. Their keys will however remain untampered.

These will only be able to redact or mask top-level fields, not nested items.

Note: These "meta" items will not themselves show up in your logs.

Example usage:

const metadataConfig = {
  userId: 'Sam Person',
  secretValue: 'sj02jd-m3982',
  redactedKeys: ['userId'],
  maskedValues: ['secretValue']
};
const logger = new MikroLog({ metadataConfig });
const log = logger.log('Checking...');

/**
 * The log will be similar to:
{
  message: 'Checking...',
  secretValue: 'MASKED'
}
*/

License

FOSSA Status

You might also like...

Context-carrying logger with conditional methods.

logger Provides a simple logging interface as well as a LogContext class which carries a context around. Installation npm install @rocicorp/logger Us

May 20, 2022

Base provides advanced Promise Queue Manager, Custom Console Logger and other utilities.

Base Base provides frequently used functionality like cutome logger, response helper, Custom Promise and Instance composer. These are used in almost a

Jun 14, 2022

An elegant console logger.

An elegant console logger.

kons An elegant console logger. Features Tiny (Minified + Gzipped ≈ 0.1kB). Beautiful. Easy to use. Customizable. TypeScript type declarations include

Aug 13, 2022

Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Dec 6, 2022

CA9.io Portal Seed Server. Makes sure the project files are always accessable.

Torrent Seed Server What is this about? This project helps users of CA9.io Metaverse to keep their files and addons permanently available. Since we us

Feb 3, 2022

long-term project untuk menunggu lebaran (update versi always bro) wkwkwk

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Jan 3, 2023

Generate deterministic fake values: The same input will always generate the same fake-output.

Generate deterministic fake values: The same input will always generate the same fake-output.

import { copycat } from '@snaplet/copycat' copycat.email('foo') // = '[email protected]' copycat.email('bar') // = 'Thurman.Schowalter668@

Dec 30, 2022

A pomodoro web app with features like always on top PIP mode!

A pomodoro web app with features like always on top PIP mode!

Tomodoro A pomodoro web app with always on top mode! Features: Clean UI(Inspired from other pomodoro apps like Pomotroid) Themes Works Offline PIP/Alw

Dec 24, 2022

Generate smooth, consistent and always-sexy box-shadows, no matter the size, ideal for design token generation.

Generate smooth, consistent and always-sexy box-shadows, no matter the size, ideal for design token generation.

smooth-shadow Generate smooth, consistent and always-sexy box-shadows, no matter the size, ideal for design token generation. Demo As Tobias already p

Oct 15, 2022
Comments
Releases(v2.1.11)
  • v2.1.11(Nov 17, 2022)

    What's Changed

    • Ensure cold start check is only done in start()
    • Add tests
    • Cleanup tests
    • Add isColdStart to Metadata type
    • Allow 0 and false to be retained in filterMetadata()

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.10...v2.1.11

    Source code(tar.gz)
    Source code(zip)
  • v2.1.10(Nov 15, 2022)

    What's Changed

    • fix(): spread dynamic metadata before static metadata
    • Remove unneeded ts-ignores
    • Try adding Foresight to CI
    • Update SECURITY
    • fix(): ensure start function will take in all inputs when resetting
    • Update docs

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.9...v2.1.10

    Source code(tar.gz)
    Source code(zip)
  • v2.1.9(Nov 14, 2022)

    What's Changed

    • Revert back to previous private variable solution on checking if it's a cold start

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.8...v2.1.9

    Source code(tar.gz)
    Source code(zip)
  • v2.1.8(Nov 14, 2022)

    What's Changed

    • Attempt to use process environment variable to contain IS_COLD_START
    • Add tests
    • Cleanup tests

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.7...v2.1.8

    Source code(tar.gz)
    Source code(zip)
  • v2.1.7(Nov 11, 2022)

  • v2.1.6(Nov 10, 2022)

    What's Changed

    • Fix isColdStart field being removed if false - move it to later insertion stage so it's always present

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.5...v2.1.6

    Source code(tar.gz)
    Source code(zip)
  • v2.1.5(Nov 10, 2022)

    What's Changed

    • Add isColdStart field on log (useful in serverless circumstances)

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.4...v2.1.5

    Source code(tar.gz)
    Source code(zip)
  • v2.1.4(Nov 7, 2022)

    What's Changed

    • Remove lifecycleStage from metadata
    • Rename route to resource
    • Make dataSensitivity a string and therefore more flexible

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.3...v2.1.4

    Source code(tar.gz)
    Source code(zip)
  • v2.1.3(Oct 22, 2022)

    What's Changed

    • Add setCorrelationId() functionality and also add correlationId field to enrich() method
    • Update README with instructions on the usage of above features

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.2...v2.1.3

    Source code(tar.gz)
    Source code(zip)
  • v2.1.2(Oct 22, 2022)

    What's Changed

    • Add isDebugLogSampled()
    • Update README with new order, where "Logging" section is now before "Configuration"
    • Update README with examples of how to use the isDebugLogSampled() functionality, also cross-boundary

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.1...v2.1.2

    Source code(tar.gz)
    Source code(zip)
  • v2.1.1(Oct 20, 2022)

    What's Changed

    • Add support for setting custom HTTP status codes on all types of logs
    • Update dependencies
    • Minor clean up of comments

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.1.0...v2.1.1

    Source code(tar.gz)
    Source code(zip)
  • v2.1.0(Oct 1, 2022)

    What's Changed

    • Add support for setting the DEBUG sampling rate
    • Bump (developer) dependencies
    • Update docs
    • Add tests
    • Restructure metadataUtils.ts to infrastructure folder

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v2.0.0...v2.1.0

    Source code(tar.gz)
    Source code(zip)
  • v2.0.0(Jul 25, 2022)

    What's Changed

    • Update API
    • Update implementation to use singleton rather than process.env
    • Add security section to docs
    • sync timestamp formats between MikroLog and MikroTrace
    • update docs
    • add support for alphabetically sorted logs
    • refactor(): use private writeLog method to reduce duplication of process.stdout.write() calls in MikroLog entity
    • docs(typedoc): add package script for creating docs with Typedoc

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v1.0.2...v2.0.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Jul 10, 2022)

    This version contains a number of smaller refactorings, most interestingly when it comes to making a better check for valid falsy values in log key-value pairs. MikroLog will now retain (as valid lines) anything that has false or 0 as values.

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v1.0.1...v1.0.2

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Jul 10, 2022)

    This release includes a bit of refactoring, clean-up and most crucially, it addresses an oversight in version 1.0.0 in where the setting of dynamic metadata was completely missed.

    Full Changelog: https://github.com/mikaelvesavuori/mikrolog/compare/v1.0.0...v1.0.1

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Jul 10, 2022)

Owner
Mikael Vesavuori
Cloud Software Architect + Technical Standards Lead at Polestar
Mikael Vesavuori
MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are still part of a flow in the AWS cloud remote.

MerLoc MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are

Thundra 165 Dec 21, 2022
AWS Lambda & Serverless - Developer Guide with Hands-on Labs. Develop thousands line of aws lambda functions interact to aws serverless services with real-world hands-on labs

AWS Lambda & Serverless - Developer Guide with Hands-on Labs UDEMY COURSE WITH DISCOUNTED - Step by Step Development of this Repository -> https://www

awsrun 35 Dec 17, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
This is a single page web application that keeps tracks of books. Book details captured are the book title, author and ISBN. User can add a book, view a list of books and also remove any un wanted books.

Project Name This is a single page web application that keeps tracks of books. Book details captured are the book title, author and ISBN. User can add

Olivier 6 Nov 20, 2022
JCS (JSON Canonicalization Scheme), JSON digests, and JSON Merkle hashes

JSON Hash This package contains the following JSON utilties for Deno: digest.ts provides cryptographic hash digests of JSON trees. It guarantee that d

Hong Minhee (洪 民憙) 13 Sep 2, 2022
Package fetcher is a bot messenger which gather npm packages by uploading either a json file (package.json) or a picture representing package.json. To continue...

package-fetcher Ce projet contient un boilerplate pour un bot messenger et l'executable Windows ngrok qui va permettre de créer un tunnel https pour c

AILI Fida Aliotti Christino 2 Mar 29, 2022
A NodeJS Console Logger with superpowers

LogFlake LogFlake is a NodeJS console logger with superpowers. It has the same API as the usual Console but with beautified output, a message header w

Felippe Regazio 57 Oct 9, 2022
Another logger in JS. This one offers a console.log-like API and formatting, colored lines and timestamps (or not if desired), all that with 0 dependencies.

hellog Your new logger ! hellog is a general-purpose logging library. It offers a console.log-like API and formatting, extensible type-safety colored

Maxence Lecanu 4 Jan 5, 2022