Custom Transports

Send logs to external services with custom transports.

Logixlysia allows you to send logs to external services using custom transports. By default, transports work alongside the built-in console and file logging outputs.

Output Behavior

Logixlysia supports three types of logging outputs:

  1. Console Logging - Built-in console output (can be disabled with disableInternalLogger)
  2. File Logging - Built-in file output (can be disabled with disableFileLogging)
  3. Transport Logging - Custom external services (always additive)

Controlling Outputs

  • Default behavior: Console + File (if logFilePath is set) + Transports
  • disableInternalLogger: true: Disables console logging only
  • disableFileLogging: true: Disables file logging only
  • useTransportsOnly: true: Disables both console and file logging, uses only transports

Basic Transport

Create a basic transport:

// TypeScript type definitions for transport interface
type LogLevel = 'debug' | 'info' | 'warn' | 'error'
type Transport = {
  log: (level: LogLevel, message: string, meta?: Record<string, unknown>) => void | Promise<void>
}
 
const consoleTransport: Transport = {
  log: (level, message, meta) => {
    console.log(`[${level}] ${message}`, meta)
  }
}
 
app.use(logixlysia({
  config: {
    transports: [consoleTransport]
  }
}))

External Services

Elasticsearch Transport

const elasticsearchTransport = {
  log: async (level, message, meta) => {
    try {
      const res = await fetch('http://elasticsearch:9200/logs/_doc', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          level,
          message,
          ...meta,
          timestamp: new Date().toISOString()
        })
      })
      if (!res.ok) {
        console.error('Elasticsearch transport failed', { status: res.status, statusText: res.statusText })
      }
    } catch (err) {
      console.error('Elasticsearch transport error', err)
    }
  }
}

MongoDB Transport

const mongodbTransport = {
  log: async (level, message, meta) => {
    try {
      await db.collection('logs').insertOne({
        level,
        message,
        ...meta,
        timestamp: new Date()
      })
    } catch (err) {
      console.error('MongoDB transport error', err)
    }
  }
}

Slack Transport

const slackTransport = {
  log: async (level, message, meta) => {
    if (level.toLowerCase() === 'error') {
      const webhook = process.env.SLACK_WEBHOOK_URL
      if (!webhook) return
      
      try {
        await fetch(webhook, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            text: `[${level}] ${message}\n${JSON.stringify(meta, null, 2)}`
          })
        })
      } catch (err) {
        console.error('Slack transport error', err)
      }
    }
  }
}

Multiple Transports

Use multiple transports together:

app.use(logixlysia({
  config: {
    transports: [
      consoleTransport,
      elasticsearchTransport,
      slackTransport
    ]
  }
}))

Advanced Configuration

Transport-Only Logging

To use only transports (disable console and file logging):

app.use(logixlysia({
  config: {
    useTransportsOnly: true,
    transports: [
      elasticsearchTransport,
      slackTransport
    ]
  }
}))

Note: In useTransportsOnly mode, logFilePath and console output are ignored.

Selective Output Control

// Disable console but keep file logging
app.use(logixlysia({
  config: {
    logFilePath: '/var/log/app.log',
    disableInternalLogger: true,
    transports: [elasticsearchTransport]
  }
}))
 
// Disable file logging but keep console
app.use(logixlysia({
  config: {
    // logFilePath is ignored when disableFileLogging is true
    disableFileLogging: true,
    transports: [elasticsearchTransport]
  }
}))

Best Practices

  1. Error Handling

    • Handle transport errors gracefully
    • Implement retry logic
    • Log transport failures
  2. Performance

    • Use async transports
    • Implement batching
    • Consider rate limiting
  3. Security

    • Secure transport credentials
    • Validate log data
  4. Output Management

    • Use useTransportsOnly for production environments where you only want external logging
    • Use disableFileLogging when you want console output but not file output
    • Use disableInternalLogger when you want file/transport output but not console output
    • Avoid logging secrets or PII; redact sensitive fields where unavoidable
    • Implement access control

Example Configurations

Production Setup

app.use(logixlysia({
  config: {
    useTransportsOnly: true,
    transports: [
      elasticsearchTransport,
      slackTransport
    ]
  }
}))

Development Setup

app.use(logixlysia({
  config: {
    disableInternalLogger: true,
    transports: [
      consoleTransport,
      mongodbTransport
    ]
  }
})) 

On this page