plugin architecture and flexible log system for golang

Overview

xlog

plugin architecture and flexible log system for golang

Build Status Go Report Card GoDoc

Installation

go get github.com/xfxdev/xlog

Usage

import (
    "github.com/xfxdev/xlog"
)

strLogLevel := "INFO"  // also maybe read from config.
logLevel, suc := xlog.ParseLevel(strLogLevel)
if suc == false {
    // failed to parse log level, will use the default level[INFO] instead."
}
xlog.SetLevel(logLevel)

// write log to file.
w2f, err := xlog.NewW2FileListener("")
if err != nil {
    xlog.Fatal(err)
} else {
    xlog.AddListener(w2f)
}

xlog.Info("server start...")
xlog.Debugf("ip : %v", "127.0.0.1")

Features

###Level logging

// xlog provide 6 logging levels.
const (
	PanicLevel Level = iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
)

you can call 'SetLevel' to change the log level. All the logs which level <= your set will be output. for example:

// all logs will be output because DEBUG is the max level.
xlog.SetLevel(xlog.DebugLevel) 

// only Panic/Fatal/Error logs will be output because WARN/INFO/DEBUG > ERROR.
xlog.SetLevel(xlog.ErrorLevel)

###Custom Log Layout xlog provide a flexible log layout system to custom the style of log output. What you need to do is just set the layout flags.

xlog.SetLayout('layout flags...')

xlog provide some builtin layout:

//   %y : year
//   %M : month
//   %d : day
//   %h : hour
//   %m : min
//   %s : second
//   %l : log msg
//   %L : log level
//   %F : file			eg: /a/b/c/d.go
//   %f : short file	eg: d.go
//   %i : line
//   %D : %y/%M/%d
//   %T : %h:%m:%s

You can use a combination of them, for example:

// this mean every log message will have a '[level] year/month/day hour:min:sec' perfix, eg:
xlog.SetLayout("%L %D %T %l")

// outputs:
[INFO] 2016/01/01 13:27:07 net start...
[WARN] 2016/01/01 13:28:00 ...
[DEBUG] 2016/01/01 13:28:00 accept...

// add filename and line to log message.
xlog.SetLayout("%L %D %T [%f(%i)] %l")

// outputs:
[INFO] 2016/01/01 13:27:07 [test.go:(72)] net start...
[WARN] 2016/01/01 13:28:00 [test.go:(100)] ...
[DEBUG] 2016/01/01 13:28:00 [test.go:(128)] accept...

You can use any form of combination, even meaningless thing, such as more spaces, arbitrary symbols:

xlog.SetLayout("hahaha%L | %D   %T [ABC] %l [i'm after hahaha]")

// outputs:
// notice the prefix 'hahaha', the spaces in the middle, and the suffix '[i'm after hahaha]'
hahaha[INFO] | 2017/01/07     14:09:47 [ABC] net start... [i'm after hahaha]
hahaha[WARN] | 2017/01/07     14:09:47 [ABC] ... [i'm after hahaha]
hahaha[DEBUG] | 2017/01/07     14:09:47 [ABC] accept... [i'm after hahaha]

NOTICE: If you doesn't call 'SetLayout', xlog will use '%L %D %T %l' by default.

###Output a log to different targets xlog use listener system to output the log message.

// A Listener simple typed of io.Writer
type Listener io.Writer

A logger can have multiple listeners, xlog have 2 builtin listener, which are os.Stderr and W2FileListener. xlog will output log to os.Stderr by default, but you can add W2FileListener to output the log to file.

w2f, err := xlog.NewW2FileListener("logfilePath...")
if err != nil {
    xlog.Fatal(err)
} else {
    xlog.AddListener(w2f)
}

In 'NewW2FileListener' function, xlog will use the 'logfilePath' to create log file, so please makesure your path is correct.

os.MkdirAll(filepath.Dir(logfilePath), os.ModePerm)

Also, you can give a empty path to NewW2FileListener(""), this will create log file by simple rule. for example: If your app current path is 'a/b/c', and your app name is 'testapp' Then the log file will be 'a/b/c/log/testapp_2016_01_01.log'

w2f, err := xlog.NewW2FileListener("")  // create log file at : 'a/b/c/log/test_2016_01_01.log'
if err != nil {
    xlog.Fatal(err)
}
xlog.AddListener(w2f)

You can create new listener according you need, just implement the io.Writer interface.

type Writer interface {
	Write(p []byte) (n int, err error)
}

###Thread safety By default xlog is protected by mutex, so you can output logs in multiple goroutines.

The plugin serves as a starting point for writing a Mattermost plugin

Plugin Starter Template This plugin serves as a starting point for writing a Mattermost plugin. Feel free to base your own plugin off this repository.

Juho Nurminen 0 Dec 10, 2021
Nhat Tran 0 Jan 4, 2022
Cf-cli-find-app-plugin - CF CLI plugin to find applications containing a search string

Overview This cf cli plugin allows users to search for application names that co

null 0 Jan 3, 2022
Gmqtt is a flexible, high-performance MQTT broker library that fully implements the MQTT protocol V3.1.1 and V5 in golang

中文文档 Gmqtt News: MQTT V5 is now supported. But due to those new features in v5, there area lots of breaking changes. If you have any migration problem

null 554 Jan 14, 2022
Go network programming framework, supports multiplexing, synchronous and asynchronous IO mode, modular design, and provides flexible custom interfaces

Go network programming framework, supports multiplexing, synchronous and asynchronous IO mode, modular design, and provides flexible custom interfaces。The key is the transport layer, application layer protocol has nothing to do

rick.wu 7 Nov 17, 2021
Magma is an open-source software platform that gives network operators an open, flexible and extendable mobile core network solution.

Connecting the Next Billion People Magma is an open-source software platform that gives network operators an open, flexible and extendable mobile core

Magma 1.2k Jan 17, 2022
A flexible configuration manager for Wireguard networks

Drago A flexible configuration manager for WireGuard networks Drago is a flexible configuration manager for WireGuard networks which is designed to ma

Seashell 825 Jan 14, 2022
An easy-to-use, flexible network simulator library in Go.

ns-x An easy-to-use, flexible network simulator library for Go. Feature Programmatically build customizable and scalable network topology from basic n

Bytedance Inc. 89 Jan 22, 2022
girc is a flexible IRC library for Go

girc, a flexible IRC library for Go Status girc is fairly close to marking the 1.0.0 endpoint, which will be tagged as necessary, so you will be able

Liam Stanley 69 Nov 28, 2021
A standalone Web Server developed with the standard http library, suport reverse proxy & flexible configuration

paddy 简介 paddy是一款单进程的独立运行的web server,基于golang的标准库net/http实现。 paddy提供以下功能: 直接配置http响应 目录文件服务器 proxy_pass代理 http反向代理 支持请求和响应插件 部署 编译 $ go build ./main/p

fangyousong 4 Dec 9, 2021
Diag - Auto Diagdnosis error log for golang

Diag 1:自动诊断产品日志中的报错信息,进行知识库比对,并给出解决方案 \n 2:prometheus 本地数据 api 拉取展示,应对 sm0.2 版本或

null 0 Jan 12, 2022
A sample web API in GO (with GIn) under a domain driven architecture.

Golang Sample API Domain Driven Design Pattern 1. About This sample project presents a custom made domain driven API architecture in Golang using the

George Batagiannis ⚡ 1 Jan 10, 2022
Eventproc - A proof-of-concept for exploring event based architecture utilizing bi-directional gRPC streams

eventproc A proof-of-concept for exploring event based architecture utilizing bi

null 0 Jan 18, 2022
Header Block is a middleware plugin for Traefik to block request and response headers which regex matched by their name and/or value

Header Block is a middleware plugin for Traefik to block request and response headers which regex matched by their name and/or value Conf

null 3 Jan 7, 2022
Developing a Traefik plugin using golang

Developing a Traefik plugin Traefik plugins are developed using the Go language. A Traefik middleware plugin is just a Go package that provides an htt

Maximilian Fischer 0 Nov 21, 2021
A Golang protobuf plugin used to generate the necessary interfaces to interact with the database

protoc-gen-go-db-enum This protobuf compiler plugin generates the Valuer and Scanner interfaces for enums defined in the proto files. It is highly bas

Hellyson Rodrigo Parteka 0 Nov 29, 2021
Http Plugin Management For Golang

Http Plugin Management Install & Use Inline plugin PluginManager Example EnvoyPlugin Example 中文 Http Plugin Management Install & Use Use the following

null 3 Dec 15, 2021
Developing a Traefik plugin with golang

Developing a Traefik plugin Traefik plugins are developed using the Go language. A Traefik middleware plugin is just a Go package that provides an htt

So Koide 1 Dec 16, 2021
Powered by Matterbridge, MatterAMXX is a plugin for AMXX that allows simple bridging between your game servers, Mattermost, IRC, XMPP, Gitter, Slack, Discord, Telegram, and more.

Powered by Matterbridge, MatterAMXX is a plugin for AMXX that allows simple bridging between your game servers, Mattermost, IRC, XMPP, Gitter, Slack, Discord, Telegram, and more.

Gabriel Iggy N. 7 Nov 6, 2021