0.0.8 • Published 9 months ago

@intellihr/lambda-logger v0.0.8

Weekly downloads
10
License
MIT
Repository
gitlab
Last release
9 months ago

Lambda Logger

A logger which complies with our logging standard (see Logging Format)

Get Started

  1. Install

    yarn add @intellihr/lambda-logger
    # or
    npm i @intellihr/lambda-logger
  2. Usage

    import { logger } from '@intellihr/lambda-logger'

Environment Variables

Logger uses environment variables to set default values. These are optional.

Available variables:

NameDefaultAvailable ValuesDescription
LOGGING_LEVELnullemerg,alert,crit, err, warning, notice, info, debugIf set, log only if level less than or equal to this level (see: Logging Level)
LOGGINGconsoleconsole, fileThis is where the logger puts its output.
LOG_FILE_LOCATION./output/log.txtAny valid path in stringOnly used when LOGGING=file. The path where the output log is stored
SERVICEnullstringSet default value for service
STAGEnullstringSet default value for environment
HOSTnullstringSet default value for host
REGIONnullstringSet default value for region
IS_OFFLINEnullstringIf not set, it will output the log using stdout to avoid extra prefix in AWS Lambda

For serverless, you can set the environment variables in the serverless.yml

provider:
  environment:
    LOGGING_LEVEL: ${env:LOGGING_LEVEL, 'info'}
    LOGGING: ${env:LOGGING, 'console'}
    SERVICE: ${self:service}
    STAGE: ${self:custom.stage}
    REGION: ${self:custom.region}

Logging Level

const levelMap = {
  emerg: 0,
  alert: 1,
  crit: 2,
  err: 3,
  warning: 4,
  notice: 5,
  info: 6,
  debug: 7
}

Logger Options

These are accepted as part of our standard (see Logging Format)

const {
  level,
  service,
  environment,
  region,
  host,
  timestamp,
  user,
  path,
  tags,
  status,
  message,
  data,
  tenant
} = options

logger.log(options)
// or
logger(options).log()

Logger Methods

See Logger Options for all the accepted options

import { logger } from '@intellihr/lambda-logger'

logger.emergency('message', options)
logger.alert('message', options)
logger.critical('message', options)
logger.error('message', options)
logger.warning('message', options)
logger.notice('message', options)
logger.info('message', options)
logger.debug('message', options)
logger.log(options)

Usage

See Logger Options for all the accepted options

See Logger Methods for all the available methods

Simple:

import { logger } from '@intellihr/lambda-logger'

// The belows are printing the same log
// case 1
const myLogger = logger({ tags: ['test'] })
myLogger.info('This is info')

// case 2
logger.info('This is info', { tags: ['test'] })

// case 3
logger.log({ level: 'info', tags: ['test'], message: 'This is info'})

// case 4
const myLogger = logger({ tags: ['test'] })
myLogger.log({ level: 'info', message: 'This is info'})

Advanced:

import { logger } from '@intellihr/lambda-logger'

const testLog = logger({ tags: ['test'] })

const logInfo = testLog.info
const logError = testLog.error

// Both with tags: ['test']
logInfo('This is info')
logError('This is error')

Example to create your own logger:

import { logger } from '@intellihr/lambda-logger'

class MyLogger {
  constructor (options) {
    this._logger = logger({
      service: 'my-service',
      ...options
    })
  }

  info (options) {
    this._logger.info(options)
  }
}

// This will attach both service and tags
const myLogger = newLogger({ tags: ['oh-my'] })
myLogger.info('This is an info')

Local Development

Run inside docker

docker-compose run --rm code /bin/sh
# or without mounting node_module
docker-composer run --rm code-mac /bin/sh

Test

yarn test

Lint

yarn lint
0.0.8

9 months ago

0.0.6

2 years ago

0.0.5

5 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.1-0

6 years ago