包详细信息

logging-ts

gcanti42.9kMIT0.3.4

Composable loggers for TypeScript

typescript, logging, functional-programming, fp-ts

自述文件

Adapted from purescript-logging

fp-ts and TypeScript compatibility

logging-ts version required fp-ts version required TypeScript version
0.3.0 2.0.0+ 3.5+
0.2.0 1.7.0+ 2.8.0+

Usage

From purescript-logging's README

A logger receives records and potentially performs some effects. You can create a logger from any function (a: A) => HKT<M, void> for any A and M.

Unlike most other logging libraries, logging-ts has no separate concepts "loggers" and "handlers". Instead, loggers can be composed into larger loggers using the Semigroup instance. Loggers can also be transformed using contramap (for transforming records) and filter (for filtering records). An example use case might be the following:

import * as C from 'fp-ts/lib/Console'
import * as D from 'fp-ts/lib/Date'
import { chain, IO } from 'fp-ts/lib/IO'
import { pipe } from 'fp-ts/lib/pipeable'
import * as L from '../src/IO'

type Level = 'Debug' | 'Info' | 'Warning' | 'Error'

interface Entry {
  message: string
  time: Date
  level: Level
}

function showEntry(entry: Entry): string {
  return `[${entry.level}] ${entry.time.toLocaleString()} ${entry.message}`
}

function getLoggerEntry(prefix: string): L.LoggerIO<Entry> {
  return entry => C.log(`${prefix}: ${showEntry(entry)}`)
}

const debugLogger = L.filter(getLoggerEntry('debug.log'), e => e.level === 'Debug')
const productionLogger = L.filter(getLoggerEntry('production.log'), e => e.level !== 'Debug')
const logger = L.getMonoid<Entry>().concat(debugLogger, productionLogger)

const info = (message: string) => (time: Date): IO<void> => logger({ message, time, level: 'Info' })
const debug = (message: string) => (time: Date): IO<void> => logger({ message, time, level: 'Debug' })

const program = pipe(
  D.create,
  chain(info('boot')),
  chain(() => D.create),
  chain(debug('Hello!'))
)

program()
/*
production.log: [Info] 10/4/2019, 12:44:48 PM boot
debug.log: [Debug] 10/4/2019, 12:44:48 PM Hello!
*/

Documentation

更新日志

Changelog

Tags:

  • [New Feature]
  • [Bug Fix]
  • [Breaking Change]
  • [Documentation]
  • [Internal]
  • [Polish]
  • [Experimental]

Note: Gaps between patch versions are faulty/broken releases. Note: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.

0.3.4

  • New Feature
    • IO
      • add withLogger combinator, #21 (@waynevanson)

0.3.3

  • New Feature
    • add Logger2, #19 (@tetsuo)

0.3.2

  • New Feature
    • add async logger, #18 (@mstn)

0.3.1

  • New Feature
    • add build in ES6 format (@gcanti)

0.3.0

  • Breaking Change
    • upgrade to fp-ts@2.x (@gcanti)

0.2.0

  • Breaking Change
    • move fp-ts to peerDependencies (@mstn)

0.1.0

  • Breaking Change
    • upgrade to fp-ts@1.x.x (@gcanti)

0.0.1

Initial release