0.3.31-main.84be8c194 • Published 3 years ago

@salto-io/logging v0.3.31-main.84be8c194

Weekly downloads
8,486
License
Apache-2.0
Repository
github
Last release
3 years ago

Salto Logging

Features

  • Simple API (compared to other logging libraries)
  • Configuration via env vars or API:
    • JSON or text format
    • Outputs to stdout or file
    • Filter specific loggers or pattern
  • Testable log calls
  • util.inspect-like formatting: log.info('hello %o', world)
  • Logs JS Errors with stack

Installing

yarn add @salto/logging

or

npm install @salto/logging

Logging

Getting a logger instance

import { logger } from '@salto/logging'

const log = logger(module) // namespace taken from module filename
const log = logger('my.namespace') // namespace set explicitly

Using the logger

Explicit log levels

log.trace('Detailed message')
log.debug('My debug message')
log.info('My object: %o', { hello: 'world' })
log.warn(new Error('oops'))
log.error('This message has extra', { extra: true })

Dynamic log level

import { LogLevel } from '@salto/logging'
const level: LogLevel = howVerboseShouldItBe()
log.log(level, 'My object %o', { hello: 'world' })

Formatting messages

All the log methods accept a printf-like format string, formatted using util.inspect:

log.info('My object: %o', { hello: 'world' })

Logging Error objects

If a JavaScript Error object is specified as the message, its stack property will be logged (which includes the Error's message).

log.warn(new Error('oops')) // logs "Error: oops" along with the stack trace

Logging extra properties

Object specified after formatting arguments will be included in the log's JSON output.

log.error('This message has extra', { extra: true })

Assigning log tags per namespace logger

Assigning tags to future log message from any logger with the same namespace. In order to empty assigned tags, simply run the function with undefined. Tags can be boolean, strings, objects or callback functions that return the latter.

log.assignTags({ requestId: '5', contextData: 4, calcAtRuntime: () => 'caclulated at runtime' })
log.assignTags(undefined) // Empties all assigned tags

Assigning log tags globally

Assigning tags to future log message from all loggers. In order to empty assigned tags, simply run the function with undefined. Tags can be boolean, strings, objects or callback functions that return the latter.

log.globalAssignTags({ requestId: '5', contextData: 4, calcAtRuntime: () => 'caclulated at runtime' })
log.globalAssignTags(undefined) // Empties all assigned tags

Assigning logTime decorator globally

Assigning logTime decorator will allow to add a custom hook to all logTime calls.

log.assignGlobalLogTimeDecorator((timedFunc, description) => {
  // some tracing logic
  return timedFunc
})

Configuring the loggers

The configuration can be changed via env vars.

Variables containing empty string values are treated as undefined.

All the loggers share a single configuration with the following properties:

minLevel: 'info' | 'debug' | 'trace' | 'warn' | 'error' | 'none'

Environment variable:

SALTO_LOG_LEVEL=info

Log calls below the specified level will be ignored.

If 'none' is specified, all calls will be ignored.

Default: none

filename: string | null

Environment variable:

SALTO_LOG_FILE=/tmp/my-package.log

Write the logs to the specified file.

If an empty value is specified, logs will be written to stdout.

Default: '' (write to stdout)

format: 'text' | 'json'

Environment variable:

SALTO_LOG_FORMAT=json

'text': Write logs as text lines.

'json': Write logs as JSON objects, separated by newlines. In this mode the log will include any extra properties:

log.info('Example for extra props written in JSON mode', { extra: ['x', 'y'] })

Default: 'text'

namespaceFilter: string | () => boolean

Environment variable:

SALTO_LOG_NS='mypackage*'

If a string is specified, it is parsed as a glob - only logs having matching namespaces will be written.

When configuring the logger using the .configure API call, a function accepting the string namespace and returning boolean may be specified.

Default: '' (no filtering)

colorize: boolean | null

Environment variable:

SALTO_LOG_COLOR=true

Override colorization in output.

Default: '' - colorize if writing to stdout and the stream supports color.

globalTags: LogTags | null

Environment variable:

SALTO_LOG_GLOBAL_TAGS='{"requestId":20}'

Add tags to all log messages. When configuring this variable via the CLI, make sure this variable is a JSON.

Default: {} - Doesn't add any tags to log messages.

maxJsonLogChunkSize: number | null

Environment variable:

SALTO_LOG_MAX_JSON_LOG_CHUNK_SIZE=3072 # 3K

Configure the max chunk size for the formatted json log message.

Default: 200 * 1024 - 200K

Supported formatting: Receives only number which signifies the allowed bytes amount

maxTagsPerLogMessage: number | null

Environment variable:

SALTO_LOG_MAX_TAGS_PER_LOG_MESSAGE=100

Configure the max amount of tags per message.

Default: 100

Supported formatting: Receives only number which signifies the allowed amount of tags per log line

setting the logging from the programmatic API

logger.setMinLevel('info')

Testing for log calls

Logger creations are memoized using the specified namespace: Multiple logger(namespace) calls result the same logger. So the logger can be initialized statically in the code, and retrieved in the test by specifying the same namespace.

Example using jest

myModule.ts:

import { logger } from '@salto/logging'

const log = logger('my.module')

export const hello = () => log.info('hello')

myModule.test.ts:

import { logger } from '@salto/logging'
import { hello } from './myModule'

describe('log calls', () => {
  let spyLogger: jest.SpyInstance

  beforeEach(() => {
    const testLogger = logger('my.module') // same namespace
    spyLogger = jest.spyOn(testLogger, 'info')
    hello()
  })

  it('calls logger correctly', () => {
    expect(spyLogger).toHaveBeenCalledWith('hello')
  })
})
0.5.0

10 months ago

0.5.2

8 months ago

0.5.1

9 months ago

0.4.9

10 months ago

0.4.8

10 months ago

0.4.7

11 months ago

0.4.6

11 months ago

0.5.1-daniel.1

9 months ago

0.5.0-daniel.1

9 months ago

0.5.0-daniel.2

9 months ago

0.5.0-daniel.3

9 months ago

0.4.4

1 year ago

0.4.3

1 year ago

0.4.2

1 year ago

0.4.1

1 year ago

0.3.49-dantal

2 years ago

0.4.0

1 year ago

0.3.61

1 year ago

0.3.60

1 year ago

0.3.53

2 years ago

0.3.52

2 years ago

0.3.51

2 years ago

0.3.50

2 years ago

0.3.59

1 year ago

0.3.58

1 year ago

0.3.57

1 year ago

0.3.56

2 years ago

0.3.55

2 years ago

0.3.54

2 years ago

0.3.42

2 years ago

0.3.41

2 years ago

0.3.49

2 years ago

0.3.48

2 years ago

0.3.47

2 years ago

0.3.46

2 years ago

0.3.45

2 years ago

0.3.44

2 years ago

0.3.43

2 years ago

0.3.40

2 years ago

0.3.39

2 years ago

0.3.38

2 years ago

0.3.37

3 years ago

0.3.36

3 years ago

0.3.35

3 years ago

0.3.34

3 years ago

0.3.33

3 years ago

0.3.31

3 years ago

0.3.30

3 years ago

0.3.32

3 years ago

0.3.29

3 years ago

0.3.28

3 years ago

0.3.27

3 years ago

0.3.27-dantal.1

3 years ago

0.3.28-dantal.1

3 years ago

0.3.28-dantal.9

3 years ago

0.3.26

3 years ago

0.3.25

3 years ago

0.3.24

3 years ago

0.3.23-dantal.1

3 years ago

0.3.23

3 years ago

0.3.24-dantal.5

3 years ago

0.3.24-dantal.4

3 years ago

0.3.24-dantal.3

3 years ago

0.3.24-dantal.2

3 years ago

0.3.24-dantal.1

3 years ago

0.3.24-dantal.7

3 years ago

0.3.24-dantal.6

3 years ago

0.3.20

3 years ago

0.3.22

3 years ago

0.3.21

3 years ago

0.3.19

3 years ago

0.3.18

3 years ago

0.3.17

3 years ago

0.3.19-soap.3

3 years ago

0.3.19-soap.0

3 years ago

0.3.19-soap.2

3 years ago

0.3.19-soap.1

3 years ago

0.3.16

3 years ago

0.3.15

4 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.9

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.2.15

4 years ago

0.2.2-alpha.84

4 years ago

0.2.14

4 years ago

0.2.2-alpha.83

4 years ago

0.2.2-alpha.82

4 years ago

0.2.13

4 years ago

0.2.2-alpha.81

4 years ago

0.2.2-alpha.80

4 years ago

0.2.2-alpha.79

4 years ago

0.2.2-alpha.78

4 years ago

0.2.2-alpha.77

4 years ago

0.2.2-alpha.76

4 years ago

0.2.12

4 years ago

0.2.23-alpha.0

4 years ago

0.2.2-alpha.75

4 years ago

0.2.2-alpha.74

4 years ago

0.2.2-alpha.73

4 years ago

0.2.11

4 years ago

0.2.2-alpha.72

4 years ago

0.2.2-alpha.71

4 years ago

0.2.2-alpha.70

4 years ago

0.2.2-alpha.69

4 years ago

0.2.2-alpha.68

4 years ago

0.2.2-alpha.67

4 years ago

0.2.2-alpha.66

4 years ago

0.2.2-alpha.65

5 years ago

0.2.2-alpha.63

5 years ago

0.2.2-alpha.64

5 years ago

0.2.10

5 years ago

0.2.2-alpha.61

5 years ago

0.2.2-alpha.62

5 years ago

0.2.2-alpha.60

5 years ago

0.2.2-alpha.58

5 years ago

0.2.2-alpha.59

5 years ago

0.2.2-alpha.57

5 years ago

0.2.2-alpha.56

5 years ago

0.2.2-alpha.55

5 years ago

0.2.9

5 years ago

0.2.2-alpha.54

5 years ago

0.2.2-alpha.52

5 years ago

0.2.2-alpha.53

5 years ago

0.2.2-alpha.51

5 years ago

0.2.2-alpha.50

5 years ago

0.2.2-alpha.49

5 years ago

0.2.2-alpha.47

5 years ago

0.2.2-alpha.48

5 years ago

0.2.8

5 years ago

0.2.2-alpha.45

5 years ago

0.2.2-alpha.46

5 years ago

0.2.7

5 years ago

0.2.2-alpha.43

5 years ago

0.2.2-alpha.44

5 years ago

0.2.2-alpha.41

5 years ago

0.2.2-alpha.42

5 years ago

0.2.2-alpha.40

5 years ago

0.2.2-alpha.39

5 years ago

0.2.2-alpha.37

5 years ago

0.2.2-alpha.38

5 years ago

0.2.6

5 years ago

0.2.2-alpha.36

5 years ago

0.2.2-alpha.35

5 years ago

0.2.2-alpha.34

5 years ago

0.2.2-alpha.33

5 years ago

0.2.2-alpha.32

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.2-alpha.3

5 years ago

0.2.2-alpha.31

5 years ago

0.2.2-alpha.2

5 years ago

0.2.2-alpha.0

5 years ago

0.2.2-alpha.1

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.41

5 years ago

0.1.40

5 years ago

0.1.39

5 years ago

0.1.38

5 years ago

0.1.37

5 years ago

0.1.36

5 years ago

0.1.35

5 years ago

0.1.34

5 years ago

0.1.33

5 years ago

0.1.32

5 years ago

0.1.31

5 years ago

0.1.30

5 years ago

0.1.29

5 years ago

0.1.28

5 years ago

0.1.27

5 years ago

0.1.26

5 years ago

0.1.25

5 years ago

0.1.24

5 years ago

0.1.23

5 years ago

0.1.22

5 years ago

0.1.21

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

0.1.4

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.1.3

6 years ago