Giter Site home page Giter Site logo

iudelsmann / winston-slack-webhook-transport Goto Github PK

View Code? Open in Web Editor NEW

This project forked from theapplefreak/winston-slack-webhook-transport

0.0 1.0 0.0 79 KB

A Slack transport for Winston 3 that logs to a channel via webhooks

License: MIT License

JavaScript 100.00%

winston-slack-webhook-transport's Introduction

winston-slack-webhook-transport

A Slack transport for Winston 3+ that logs to a channel via webhooks.

NPM

Installation

npm install winston winston-slack-webhook-transport

Usage

Set up with transports

const winston = require("winston");
const SlackHook = require("winston-slack-webhook-transport");

const logger = winston.createLogger({
	level: "info",
	transports: [
		new SlackHook({
			webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx"
		})
	]
});

logger.info("This should now appear on Slack");

Set up by adding

const winston = require("winston");
const SlackHook = require("winston-slack-webhook-transport");

const logger = winston.createLogger({});

logger.add(SlackHook, {webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx"});

Options

  • webhookUrl - Slack incoming webhook URL. This can be from a basic integration or a bot. REQUIRED
  • channel - Slack channel to post message to.
  • username - Username to post message with.
  • iconEmoji - Status icon to post message with. (interchangeable with iconUrl)
  • iconUrl - Status icon to post message with. (interchangeable with iconEmoji)
  • formatter - Custom function to format messages with. This function accepts the info object (see Winston documentation) and must return an object with at least one of the following three keys: text (string), attachments (array of attachment objects), blocks (array of layout block objects). These will be used to structure the format of the logged Slack message. By default, messages will use the format of [level]: [message] with no attachments or layout blocks.
  • level - Level to log. Global settings will apply if this is blank.
  • unfurlLinks - Enables or disables link unfurling. (Default: false)
  • unfurlMedia - Enables or disables media unfurling. (Default: false)
  • mrkdwn - Enables or disables mrkdwn formatting within attachments or layout blocks (Default: false)

Message formatting

winston-slack-webhook-transport supports the ability to format messages using Slack's message layout features. To do this, supply a custom formatter that supplies the requisite object structure to create the desired layout.

const winston = require("winston");
const SlackHook = require("winston-slack-webhook-transport");
 
const logger = winston.createLogger({
    level: "info",
    transports: [
        new SlackHook({
			webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx",
			formatter: info => {
				return {
					text: `${info.level}: ${info.message}`,
					attachments: [
						{
							text: "Or don't pass anything. That's fine too"
						}
					],
					blocks: [
						{
							type: "section",
							text: {
								type: "plain_text",
								text: "You can pass more info to the formatter by supplying additional parameters in the logger call"
							}
						}
					]
				}
			}
        })
    ]
});

logger.info("Definitely try playing around with this.")

winston-slack-webhook-transport's People

Contributors

theapplefreak avatar

Watchers

James Cloos 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.