0.7.14 • Published 3 months ago

@x/log v0.7.14

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

@x/log

Simple, fast, object based logging for the @x platform.

Installation

yarn add @x/log

Usage

import logger from '@x/log'

const log = logger()
log.error(new Error('An error occurred'))

This creates a logger object that will use the default console log writer and only log entries with a level of info or higher.

Log Functions

The created object exposes a function for each log level:

LevelValue
error1
warn2
info3
debug4
trace5

Each of these functions accepts a variable number of parameters that will be merged in to the final log entry. A level property will also be set based on the function called. String arguments will be assigned to the message property.

Additionally, the following utility functions are exposed:

log(...args)

Log a new entry using the provided arguments without a level property.

child(scope)

Create a child logger that will merge the provided object into each log entry.

Options

The following options can be passed to the logger constructor:

NameTypeDescription
levelstring/numberOnly log messages with the level specified or higher
scopeobjectAn initial object to be merged in to each log entry
levelsobjectA mapping of level names to values
filterfunctionA function that is passed a log entry to determine whether or not to log
serializersobjectAn array of objects containing test and serialize functions
writersobjectAn array of writer constructor functions
timestampPropertystringProperty to attach current timestamp to. Set to falsy value to disable. Default: "timestamp"
attachUserAgentbooleanAttach browser userAgent to logs
redirectConsolebooleanRedirect any console output to the logger
mergeFieldsstring[]An array of property names to be merged from log parameters into entry instead of overwriting
transformfunctionAllows modification of the final log entry

Static Module Properties

For convenience, the top level module exposes the following additional properties:

NameDescription
defaultOptionsThe default configuration options
defaultWriterThe default log writer used
defaultSerializersThe default set of serializers used
writersThe available set of log writers

Built In Writers

The following writers are available on the writers static property:

console

The console writer will attempt to make use of native console logging features such as colorization and accepts the following options:

NameTypeDefaultDescription
timestampPropertystringtimestampProperty name to use to extract logged timestamp from
excludePropertiesstringAn array of property names to omit from display
filterfunction(entry)An additional filter function specific to the writer
levelstringAn additional level filter specific to the writer

file

The file writer simply outputs log entries to a specified file, one per line. It accepts the following options:

NameTypeDefaultDescription
destinationstringThe file path to write to
rotatebooleanfalseCreate a new file with the current date appended for each day

Implementing Custom Writers

A writer implementation that can be passed to the writers collection should be a constructor function that is passed the global options object and returns the actual writer function that is passed each log entry.

A typical implementation would look something like:

const httpLogWriter = userOptions => globalOptions => {
  // providing userOptions introduces a new top level constructor - this is optional but used here to configure the URL
  // the entry has already been passed through relevant serializers, there is no need to deal with error objects
  return entry => fetch(userOptions.url, { method: 'POST', body: JSON.stringify(entry) })
}

This would be consumed like follows:

const log = logger({
  writers: [httpLogWriter({ url: 'https://some.logging.domain/my/logging/endpoint' })]
})

License

The MIT License (MIT)

Copyright © 2022 Dale Anderson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.7.14

3 months ago

0.7.11

8 months ago

0.7.12

7 months ago

0.7.2

12 months ago

0.7.1

12 months ago

0.7.4

12 months ago

0.7.3

12 months ago

0.7.0

12 months ago

0.7.10

11 months ago

0.7.9

11 months ago

0.7.6

11 months ago

0.7.5

11 months ago

0.7.8

11 months ago

0.7.7

11 months ago

0.6.34

1 year ago

0.6.36

1 year ago

0.6.35

1 year ago

0.6.21

2 years ago

0.6.20

2 years ago

0.6.23

2 years ago

0.6.22

2 years ago

0.6.29

2 years ago

0.6.28

2 years ago

0.6.25

2 years ago

0.6.24

2 years ago

0.6.27

2 years ago

0.6.26

2 years ago

0.6.18

2 years ago

0.6.17

2 years ago

0.6.19

2 years ago

0.6.16

2 years ago

0.6.15

2 years ago

0.6.32

1 year ago

0.6.31

2 years ago

0.6.33

1 year ago

0.6.30

2 years ago

0.6.7

2 years ago

0.6.6

2 years ago

0.6.9

2 years ago

0.6.8

2 years ago

0.6.10

2 years ago

0.6.12

2 years ago

0.6.11

2 years ago

0.4.17

2 years ago

0.4.18

2 years ago

0.6.14

2 years ago

0.6.13

2 years ago

0.6.3

2 years ago

0.5.4

2 years ago

0.6.2

2 years ago

0.5.3

2 years ago

0.6.5

2 years ago

0.6.4

2 years ago

0.5.5

2 years ago

0.5.0

2 years ago

0.6.1

2 years ago

0.5.2

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.4.16

3 years ago

0.4.15

3 years ago

0.4.14

3 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

3 years ago

0.4.10

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.7

3 years ago

0.4.5

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.3.0-private

4 years ago

0.2.13

4 years ago

0.4.0

3 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.4

4 years ago