slclogger

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2017 License: MIT Imports: 7 Imported by: 2

README

SlcLogger

Build Status

Simple and Human Friendly Slack Client for Logging Written in Go Programming Language

Install

go get "github.com/iktakahiro/slclogger"

The requirement is the Go lang 1.8+

How to Use

Basic Usage
package main

import (
	"errors"

	"github.com/iktakahiro/slclogger"
)

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

func main() {

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

	if err := something(); err != nil {
		logger.Err(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.

logger, _ := slclogger.NewSlcLogger(&slclogger.SlcLoggerParams{
    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.Err("Error Message")

You can also change the level at any time.

logger.SetLogLevel(slclogger.LevelWarn)

// The following notification is ignored.
logger.Debug("Debug Message")
Configure Options

All options are shown below.

logger, err := NewSlcLogger(&SlcLoggerParams{
    WebHookURL:   "https://hooks.slack.com/services/YOUR_WEBHOOK_URL",
    DefaultTitle: "Default Title",
    Channel:      "general",
    LogLevel:     LevelWarn,
    IconURL:      "https://example.com",
    UserName:     "My Logger",
})
Param Default Value
WebHookURL (require) --
DefaultTitle "Notification"
Channel "" (When this param is omitted, the default channel of specified WebHook 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

go test $(go list ./... | grep -v /vendor/ | grep -vE "github.com/iktakahiro/slclogger$");

Documents

Documentation

Index

Constants

View Source
const (
	LevelDebug logLevel = iota + 1
	LevelInfo
	LevelWarn
	LevelErr
)

LogLevel Constants

Variables

This section is empty.

Functions

This section is empty.

Types

type SlcErr

type SlcErr struct {
	Err  error
	Code int
}

SlcErr is a struct for Slack response error code.

func (*SlcErr) Error

func (s *SlcErr) Error() string

Error is an implementation of the error interface.

type SlcLogger

type SlcLogger struct {
	WebHookURL string
	SlcLoggerParams
	LogLevel     logLevel
	Channel      string
	UserName     string
	DefaultTitle string
	IconURL      string
}

SlcLogger is the structure detailing logger.

func NewSlcLogger

func NewSlcLogger(params *SlcLoggerParams) (*SlcLogger, error)

NewSlcLogger returns a new SLcLogger.

func (*SlcLogger) Debug

func (s *SlcLogger) Debug(message interface{}, title ...string) error

Debug is a wrapper function of sendNotification function that implicitly sets the logLevel and color.

func (*SlcLogger) Err

func (s *SlcLogger) Err(message interface{}, title ...string) error

Err is a wrapper function of sendNotification function that implicitly sets the logLevel and color.

func (*SlcLogger) Info

func (s *SlcLogger) Info(message interface{}, title ...string) error

Info is a wrapper function of sendNotification function that implicitly sets the logLevel and color.

func (*SlcLogger) SetLogLevel

func (s *SlcLogger) SetLogLevel(level logLevel)

SetLogLevel sets a logLevel to SlcLogger.LogLevel.

func (*SlcLogger) Warn

func (s *SlcLogger) Warn(message interface{}, title ...string) error

Warn is a wrapper function of sendNotification function that implicitly sets the logLevel and color.

type SlcLoggerParams

type SlcLoggerParams struct {
	WebHookURL   string
	LogLevel     logLevel
	Channel      string
	UserName     string
	DefaultTitle string
	IconURL      string
}

SlcLoggerParams is the set of parameters that can be used when creating a new SlcLogger.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL