@intellihr/lambda-logger

A logger which complies with intelliHR logging standard

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@intellihr/lambda-logger
0.0.67 months ago4 years agoMinified + gzip package size for @intellihr/lambda-logger in KB

Readme

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

Get Started

  1. Install
```bash
yarn add @intellihr/lambda-logger
# or
npm i @intellihr/lambda-logger
```
  1. Usage
```javascript
import { logger } from '@intellihr/lambda-logger'
```

Environment Variables

Logger uses environment variables to set default values. These are optional.
Available variables:
| Name | Default | Available Values | Description | | ---- | ------- | ---------------- | ----------- | | LOGGING_LEVEL | null | emerg,alert,crit, err, warning, notice, info, debug | If set, log only if level less than or equal to this level (see: Logging Level) | | LOGGING | console | console, file | This is where the logger puts its output. | | LOG_FILE_LOCATION| ./output/log.txt | Any valid path in string | Only used when LOGGING=file. The path where the output log is stored | | SERVICE | null | string | Set default value for service | | STAGE | null | string | Set default value for environment | | HOST | null | string | Set default value for host | | REGION | null | string | Set default value for region | | IS_OFFLINE | null | string | If 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