Typescript Logs

Learn how to setup logs in your Typescript application.

Setup

Open the Vigilant dashboard and generate an API key.

Install

Install the Vigilant SDK in your Typescript application.

npm install vigilant-js

Initialize

Initialize the Vigilant SDK in your Typescript application. Vigilant automatically captures console log, error, and exception messages.

import { initVigilant } from 'vigilant-js'

initVigilant({
  name: 'backend',
  token: 'tk_1234567890', // Use your API key from the dashboard
})

Logging Functions

The Vigilant SDK provides several functions to log messages directly. You can use these functions to add custom attributes to the log messages.

import { logInfo, logDebug, logWarn, logError, logTrace } from 'vigilant-js'

// Log a custom message at different log levels
logInfo('Log an info message')
logDebug('Log a debug message')
logWarn('Log a warning message')
logError('Log an error message')
logTrace('Log a trace message')

// Log with a custom attribute
logInfo('Log an info message', { user: '123' })
logDebug('Log a debug message', { user: '123' })
logWarn('Log a warning message', { user: '123' })
logError('Log an error message', { user: '123' })
logTrace('Log a trace message', { user: '123' })

Custom Attributes

You can wrap parts of your code in a callback to add custom attributes to all log messages in the callstack.

import { addAttributes, logInfo, logDebug, logWarn, logError, logTrace } from 'vigilant-js'

// All logs in the callback will have the user attribute set to 123
addAttributes({ user: '123' }, () => {
  logInfo('Log an info message')
  logDebug('Log a debug message')
  logWarn('Log a warning message')
  logError('Log an error message')
  logTrace('Log a trace message')
})

Optional Setup

The Vigilant SDK exposes a few setup options when you initialize it.

import { initVigilant } from 'vigilant-js'

initVigilant({
  // Disable all functionality
  noop: true,

  // Disable automatic capture of logs, only direct logs are captured
  autocapture: false,

  // Disables logging all logs to the console
  passthrough: false,
})