Socket
Book a DemoInstallSign in
Socket

@getcircuit/server-logging

Package Overview
Dependencies
Maintainers
19
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@getcircuit/server-logging

## Logging

unpublished
latest
npmnpm
Version
15.17.15
Version published
Maintainers
19
Created
Source

@getcircuit/server-logging

Logging

This is a logging library to be used by Circuit Backend Services.

In its essence it is a wrapper for some other logging library with a common logging interface, so we can easily change how it works without having to update the same code in all services.

Currently, we are using Pino as our logging library of choice.

Usage

This example will use an approach of having a singleton logger instance for a whole service, but this library does not enforce a single logger instance, if needed you can spawn other instances.

To use this library we need to first initialize a logger.

// logger.ts

import { createLogger } from '@getcircuit/server-logging'

export const logger = createLogger({
  level: 'info',
  pretty: true,
  enabled: true,
})

And then we can use it anywhere:

We can log a simple message

logger.info('Starting server')

Or we can log a message with extra details

logger.info('Starting server', { address: '127.0.0.1:8080' })

Or even, if we get an error, we can use the special 'error' key to automatically spread all the error properties

try {
  // try starting the server
} catch (error) {
  logger.error('Error starting server', {
    error,
    /* ...otherProperties */
  })
}

[!NOTE] This is necessary as errors in JavaScript are objects with non-enumerable properties and so JSON.stringify does not stringify the error properly if not explicitly telling it which keys to use.

Tracing

This library includes a tracing instrumentation for the logger in order to be provided to the @getcircuit/server-tracing library.

Remember that as opentelemetry instrumentations work by intercepting imports and changing them, the tracing needs to be set-up before ever importing the logging library itself.

That is why the tracing is provided in a separate export path: @getcircuit/server-logging/tracing

An example of how to use it with @getcircuit/server-tracing:

import { setupTracing } from '@getcircuit/server-tracing'
import { getLoggerTracingInstrumentation } from '@getcircuit/server-logging/tracing'

import { tracingOptionsBase } from '@Config/tracing'

setupTracing({
  ...tracingOptionsBase,
  extraInstrumentationOptions: [getLoggerTracingInstrumentation()],
})

TypeORM

This library also includes a TypeORM logger that can be used to log the queries that are being executed by TypeORM.

All queries are logged with the debug level, slow queries with the warn level, and errors with the error level.

To use it, you need to pass the logger to the createTypeORMLogger function when setting up the TypeORM connection:

import { DataSource } from 'typeorm'
import { createTypeORMLogger } from '@getcircuit/server-logging/typeorm'

import { postgres } from '$/config'
import { logger } from '$/logger'

export const sqlDataSource = new DataSource({
  type: 'postgres',
  host: postgres.host,
  port: postgres.port,
  username: postgres.username,
  password: postgres.password,
  database: postgres.database,
  entities: [
    // ...
  ],
  synchronize: false,
  logger: createTypeORMLogger(logger),
})

FAQs

Package last updated on 23 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts