Coder Social home page Coder Social logo

slclogger's Introduction

SlcLogger

Build Status

Simple and Human Friendly Slack Client for Logging/Notification written in Go

Install

go get "github.com/iktakahiro/slclogger/v2"

How to Use

Basic Usage

package main

import (
	"errors"

	"github.com/iktakahiro/slclogger/v2"
)

func something() error {
	return errors.New("an error has occurred")
}

func main() {

	logger, _ := slclogger.NewSlcLogger(&slclogger.LoggerParams{
		WebHookURL: "https://hooks.slack.com/services/YOUR_WEBHOOK_URL",
	})

	if err := something(); err != nil {
		logger.Error(err, "Error Notification")
	}
}

When you execute the above sample code, your Slack channel will receive the message.

Log Levels

The default log level is Info. You can set it when initializing a SlcLogger struct.

package main

import (
	"github.com/iktakahiro/slclogger/v2"
)

func main() {
    logger, _ := slclogger.NewSlcLogger(&slclogger.LoggerParams{
        WebHookURL: "https://hooks.slack.com/services/YOUR_WEBHOOK_URL",
        LogLevel: slclogger.LevelDebug,
    })

    logger.Debug("Debug Message")
    logger.Info("Info Message")
    logger.Warn("Warn Message")
    logger.Error("Error Message")
}

You can also change the level at any time.

package main

import (
	"github.com/iktakahiro/slclogger/v2"
)

func main() {
	logger, _ := slclogger.NewSlcLogger(&slclogger.LoggerParams{
		WebHookURL: "https://hooks.slack.com/services/YOUR_WEBHOOK_URL",
	})

    logger.SetLogLevel(slclogger.LevelWarn)

    // The following notification will be ignored.
    logger.Debug("Debug Message")
}

Configure Options

All options are shown below.

package main

import (
	"github.com/iktakahiro/slclogger/v2"
)

func main() {

    logger, err := slclogger.NewSlcLogger(&slclogger.LoggerParams{
        WebHookURL:         "https://hooks.slack.com/services/YOUR_WEBHOOK_URL",
        DefaultTitle:       "Default Title",
        DefaultChannel:     "general",
        DebugChannel:       "debug-channel",
        InfoChannel:        "info-channel",
        WarnChannel :       "warn-channel",
        ErrorChannel :      "error-channel",
        LogLevel:           slclogger.LevelWarn,
        IconURL:            "https://example.com",
        UserName:           "My Logger",
    })
}
Param Default Value
WebHookURL (require) --
DefaultTitle "Notification"
DefaultChannel "" (When this param is omitted, the default channel of specified WebHook is used.)
DebugChannel "" (When this param is omitted, the value of DefaultChannel is used.)
InfoChannel "" (When this param is omitted, the value of DefaultChannel is used.)
WarnChannel "" (When this param is omitted, the value of DefaultChannel is used.)
ErrorChannel "" (When this param is omitted, the value of DefaultChannel is used.)
LogLevel Info
IconURL ""
UseName ""

Error Handling

If you want to handle errors, use SlcErr.

if err := logger.Info("info message"); err != nil {
    if slcErr, ok := err.(*slclogger.SlcErr); ok {
        fmt.Println(slcErr)
        fmt.Println(slcErr.Code)
    }
}

Test

make test

Documents

slclogger's People

Contributors

iktakahiro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

slclogger's Issues

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.