Coder Social home page Coder Social logo

aerilym / winston-axios Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 137 KB

An axios transport for winston which allows for sending REST requests to an external API

Home Page: https://www.npmjs.com/package/winston-axios

TypeScript 63.19% JavaScript 36.81%
axios winston winston-transport

winston-axios's Introduction

Winston-Axios

Build npm version Known Vulnerabilities

An axios transport for winston which allows for sending REST requests to an external API.

Usage

Setup

const { AxiosTransport } = require('winston-axios');

or

import { AxiosTransport } from 'winston-axios';

Create a logger with an Axios Transport

const winston = require('winston');
const { AxiosTransport } = require('winston-axios');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new AxiosTransport({
      url: 'http://localhost:9999',
      path: '/log',
      auth: 'abc123',
    }),
  ],
});

Add an Axios Transport to an existing logger

const { AxiosTransport } = require('winston-axios');

logger.add(
  new AxiosTransport({
    host: 'http://localhost:9999/log'
  })
);

Receiving the log message

You can simply use logger.info(request.body); to log the request in the external API as Winston can resolve the body.

More information on body formatting is to come.

Example

A production example of using this transport is available here: photoprism-helper logger.ts

Here is the relevant code, omitting anything not relevant to the transport:

import { createLogger, transports } from 'winston';
import { AxiosTransport } from 'winston-axios';

const config = {
  levelConsole: 'info',
  url: 'http://apiurl.com'
  key: 'abc123'
}
//.code here.//

// Create a logger instance with custom settings
export const logger = createLogger({
  transports: [
    new transports.Console({ level: config.levelConsole }),
    //.code here.//
  ],
    //.code here.//
});

//.code here.//

// Add an Axios transport to the logger instance
logger.add(
  new AxiosTransport({
    url: config.url,
    path: '/log',
    auth: config.key,
  })
);

Documentation

AxiosTransportOptions

Options for Axios Transport.

AxiosTransport

Transport for Winston that sends log messages to a remote server using Axios.

AxiosTransportOptions

Options for Axios Transport.

Param Type Description
url string The url to send the logs to.
path string The path to send the logs to. The destination url will resolve to url + path.
auth string The authentication token to send with the logs. Will override any auth headers provided in headers.
authType TransportAuthType The type of authentication to use.
method TransportMethod The method to use when sending the logs.
headers AxiosRequestHeaders The headers to send with the logs.

AxiosTransport

Transport for Winston that sends log messages to a remote server using Axios.

See: AxiosTransportOptions

new AxiosTransport(options)

Param Type Description
options AxiosTransportOptions The options for the transport.

Example

const logger = createLogger({
  transports: [
    new AxiosTransport({
      url: 'http://localhost:3000',
      path: '/logs',
    }),
  ],
});
logger.log({ level: 'info', message: 'Hello World' });

winston-axios's People

Contributors

aerilym avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.