goLog

package module
v0.1.2-alpha Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2021 License: MIT Imports: 10 Imported by: 0

README

goLog

Logger for go projects

Install
go get github.com/goKLC/goLog
Import
import "github.com/goKLC/goLog"
Usage
var log goLog.LogInterface
var conf *goLog.Config
log, conf = goLog.New()

fileHandler := goLog.NewFileHandler()
fileHandler.Partition = true
fileHandler.PartitionRange = goLog.DAILY
fileHandler.Path = "log"

terminalHandler := goLog.NewTerminalHandler()

redisClient := redis.NewClient(&redis.Options{
		Addr:               "127.0.0.1:6379",
		Password:           "",
		DB:                 0,
	})

redisHandler := goLog.NewRedisHandler()
redisHandler.Client = redisClient
redisHandler.Key = "goLog"

db, _ := gorm.Open("mysql", "root:password@tcp(127.0.0.1:3306)/go_log?charset=utf8&parseTime=True")
dbHandler := goLog.NewDBHandler(db)

emailHandler := goLog.NewEmailHandler()
emailHandler.Levels = []goLog.Level{goLog.CRITICAL, goLog.EMERGENCY, goLog.ERROR}
emailHandler.Host = "smtp.mailtrap.io:25"
emailHandler.Auth = smtp.PlainAuth("", "username", "password", "smtp.mailtrap.io")
emailHandler.From = "info@goklc.org"
emailHandler.To = []string{"admin@goklc.org", "mkilic@goklc.org"}
	
config.AddHandler(fileHandler)
config.AddHandler(terminalHandler)
config.AddHandler(redisHandler)
config.AddHandler(dbHandler)
config.AddHandler(emailHandler)

*RedisHandler use github.com/go-redis/redis

*DBHandler use github.com/jinzhu/gorm

Config
conf.TimeFormat = "2006-01-02 15:04:05"

TimeFormat = log time format golang time format

Create Log

This example:

log.Info("foo message", map[string]interface{}{"foo":"bar", "baz": "foo", "subContext": map[string]interface{}{"subFoo": "subBar"}})
log.Error("foo", nil)

Output:

file path : logs/2019-11-5-app.log

[2019-11-03 00:19:00] Info: foo message
        {
          "baz": "foo",
          "foo": "bar",
          "subContext": {
            "subFoo": "subBar"
            }
         }
[2019-11-03 00:19:00] Error: foo

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() (*Log, *Config)

Types

type Config

type Config struct {
	TimeFormat string
	Handler    []HandlerInterface
}

func (*Config) AddHandler

func (c *Config) AddHandler(handler HandlerInterface)

type DBHandler

type DBHandler struct {
	DB *gorm.DB
}

func NewDBHandler

func NewDBHandler(DB *gorm.DB) *DBHandler

func (DBHandler) Write

func (dbh DBHandler) Write(l Log)

type EmailHandler

type EmailHandler struct {
	From   string
	To     []string
	Auth   smtp.Auth
	Host   string
	Levels []Level
}

func NewEmailHandler

func NewEmailHandler() *EmailHandler

func (*EmailHandler) Write

func (eh *EmailHandler) Write(log Log)

type FileHandler

type FileHandler struct {
	Partition      bool
	PartitionRange PartitionRange
	FileName       string
	Path           string
}

func NewFileHandler

func NewFileHandler() *FileHandler

func (*FileHandler) Write

func (fh *FileHandler) Write(log Log)

type HandlerInterface

type HandlerInterface interface {
	Write(log Log)
}

type Level

type Level string
const ALERT Level = "Alert"
const CRITICAL Level = "Critical"
const DEBUG Level = "Debug"
const EMERGENCY Level = "Emergency"
const ERROR Level = "Error"
const INFO Level = "Info"
const NOTICE Level = "Notice"
const WARNING Level = "Warning"

type Log

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

func (*Log) Alert

func (l *Log) Alert(message string, context map[string]interface{})

func (*Log) Critical

func (l *Log) Critical(message string, context map[string]interface{})

func (*Log) Debug

func (l *Log) Debug(message string, context map[string]interface{})

func (*Log) Emergency

func (l *Log) Emergency(message string, context map[string]interface{})

func (*Log) Error

func (l *Log) Error(message string, context map[string]interface{})

func (*Log) Info

func (l *Log) Info(message string, context map[string]interface{})

func (*Log) Notice

func (l *Log) Notice(message string, context map[string]interface{})

func (*Log) Warning

func (l *Log) Warning(message string, context map[string]interface{})

type LogInterface

type LogInterface interface {
	Debug(message string, context map[string]interface{})
	Info(message string, context map[string]interface{})
	Notice(message string, context map[string]interface{})
	Warning(message string, context map[string]interface{})
	Error(message string, context map[string]interface{})
	Critical(message string, context map[string]interface{})
	Alert(message string, context map[string]interface{})
	Emergency(message string, context map[string]interface{})
}

type PartitionRange

type PartitionRange string
const DAILY PartitionRange = "Daily"
const HOURLY PartitionRange = "Hourly"
const MONTHLY PartitionRange = "Monthly"
const YEARLY PartitionRange = "Yearly"

type RedisHandler

type RedisHandler struct {
	Client *redis.Client
	Key    string
}

func NewRedisHandler

func NewRedisHandler() *RedisHandler

func (RedisHandler) Write

func (rh RedisHandler) Write(log Log)

type TerminalHandler

type TerminalHandler struct {
}

func NewTerminalHandler

func NewTerminalHandler() *TerminalHandler

func (*TerminalHandler) Write

func (th *TerminalHandler) Write(log Log)

Jump to

Keyboard shortcuts

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