xlog

package module
v0.0.0-...-2954e74 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2019 License: MIT Imports: 6 Imported by: 0

README

XLOG

Go Report Card

XLOG is wrapper function of logrus and elogrus (elastic hook) for create log instance and use Builder Design Pattern. Easy when logging with field, And It's implement elastic hook ready to put your log to elastic host in a minuts.

Getting Started

Install with go get -u github.com/PePoDev/xlog and add init function

func init() {
    // ** Importance ** You must to call this function first
    xlog.DefaultXlogFormatter()
    // or set Formatter for logrus with yourself
}

Yeah! It's done

you can use xlog now

Example

Basic usage
package main

import "github.com/PePoDev/xlog"

func init() {
    // Init function for set logrus formatter with default xlog like
    xlog.DefaultXlogFormatter()
}

func main() {
    // log with field one time only
    xlog.CreateLog().SetField("name", "value").Info("Hello, world!")

    // or create log instance and use It for logging with field
    log := xlog.CreateLog()
    log.SetField("name_1", "value_1")
    log.SetField("name_2", "value_2")

    log.Info("hello, world!")
}
Log with Level
package main

import "github.com/PePoDev/xlog"

func init() {
    // Init function for set logrus formatter with default xlog like
    xlog.DefaultXlogFormatter()
}

func main() {
    log := xlog.CreateLog()

    log.Trace("hello, world!")
    log.Debug("hello, world!")
    log.Info("hello, world!")
    log.Warn("hello, world!")
    log.Error("hello, world!")
    log.Panic("hello, world!")
    log.Fatal("hello, world!")
}
Log with format text
package main

import "github.com/PePoDev/xlog"

func init() {
    // Init function for set logrus formatter with default xlog like
    xlog.DefaultXlogFormatter()
}

func main() {
    log := xlog.CreateLog()

    log.Tracef("hello, %s", "world!")
    log.Debugf("hello, %s", "world!")
    log.Infof("hello, %s", "world!")
    log.Warnf("hello, %s", "world!")
    log.Errorf("hello, %s", "world!")
    log.Panicf("hello, %s", "world!")
    log.Fatalf("hello, %s", "world!")
}
Add elastic hook in a minuts!
package main

import "github.com/PePoDev/xlog"

func main() {
    // Set elastic hook
    xlog.AddHookToElastic(
        "indexName",
        "host",
        logrus.InfoLevel,
        "elasticHost",
        "elasticUsername",
        "elasticPassword"
    )

    log := xlog.CreateLog()
    log.SetField("name_1", "value_1")
    log.SetField("name_2", "value_2")

    log.Info("hello, world!")
    // "hello, world!" is put to elastic Now!
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHookToElastic

func AddHookToElastic(indexName string, host string, logLevel logrus.Level, elasticHost string, elasticUsername string, elasticPassword string)

AddHookToElastic create hook for push log to elastic host

func DefaultXlogFormatter

func DefaultXlogFormatter()

DefaultXlogFormatter set Formatter with TextFormatter that XLOG like. You can use logrus package to set it with yourself

func GetFormatter

func GetFormatter() logrus.Formatter

GetFormatter return Formatter that use by XLOG

func SetFormatter

func SetFormatter(format logrus.Formatter)

SetFormatter custom logrus Formatter

Types

type XLog

type XLog struct {
	// contains filtered or unexported fields
}

XLog struct contain logrus fields

func CreateLog

func CreateLog() *XLog

CreateLog Return new XLog struct

func (XLog) Debug

func (xlog XLog) Debug(message string)

Debug will log in Debug level

func (XLog) Debugf

func (xlog XLog) Debugf(format string, a ...interface{})

Debugf will log in Debug level with format

func (XLog) Error

func (xlog XLog) Error(message string)

Error will log in Error level

func (XLog) Errorf

func (xlog XLog) Errorf(format string, a ...interface{})

Errorf will log in Error level with format

func (XLog) Fatal

func (xlog XLog) Fatal(message string)

Fatal will log in Fail level and call exit program

func (XLog) Fatalf

func (xlog XLog) Fatalf(format string, a ...interface{})

Fatalf will log in Fail level with format and call exit program

func (XLog) Info

func (xlog XLog) Info(message string)

Info will log in info level

func (XLog) Infof

func (xlog XLog) Infof(format string, a ...interface{})

Infof will log in info level with format

func (XLog) Panic

func (xlog XLog) Panic(message string)

Panic will log in panic level and call panic

func (XLog) Panicf

func (xlog XLog) Panicf(message string, a ...interface{})

Panicf will log in panic level with format and call panic

func (XLog) SetField

func (xlog XLog) SetField(fieldName string, value interface{}) *XLog

SetField for set log tag that you want

func (XLog) Trace

func (xlog XLog) Trace(message string)

Trace will log in Trace level

func (XLog) Tracef

func (xlog XLog) Tracef(format string, a ...interface{})

Tracef will log in Trace level with format

func (XLog) Warn

func (xlog XLog) Warn(message string)

Warn will log in Warning level

func (XLog) Warnf

func (xlog XLog) Warnf(format string, a ...interface{})

Warnf will log in Warning level with format

Jump to

Keyboard shortcuts

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