CoLog is a prefix-based leveled execution log for Go

Overview

Build Status godoc reference

What's CoLog?

CoLog is a prefix-based leveled execution log for Go. It's heavily inspired by Logrus and aims to offer similar features by parsing the output of the standard library log. If you don't understand what this means take a look at this picture.

CoLog showcase

But why?

An introduction and the rationale behind CoLog can be found in this blog post: https://texlution.com/post/colog-prefix-based-logging-in-golang/

Features

  • Supports hooks to receive log entries and send them to external systems via AddHook
  • Supports customs formatters (color/pain text and JSON built-in) via SetFormatter
  • Provides 6 built-in levels: trace, debug, info, warning, error, alert
  • Understands full, 3 letter, and 1 letter headers: error:, err:, e:
  • Supports custom prefixes (headers in CoLog terms) via SetHeaders and AddHeader
  • Control levels used via SetMinLevel and SetDefaultLevel
  • Supports optionally parsing key=value or key='some value' pairs
  • Supports custom key-value extractor via SetExtractor
  • Supports permanent context values via FixedValue and ClearFixedValues
  • Supports standalone loggers via NewCoLog and NewLogger
  • Compatible with existing Logrus hooks and formatters via cologrus
  • Supports Windows terminal colors via wincolog

API stability

CoLog's API is very unlikely to get breaking changes, but there are no promises. That being said, CoLog only needs to be imported by final applications and if you have one of those, you should be vendoring you dependencies in the first place. CoLog has no external dependencies, to vendor it you just need to clone this repo anywhere you want and start using it.

Usage examples

Basic usage

package main

import (
	"log"

	"github.com/comail/colog"
)

func main() {
	colog.Register()
	log.Print("info: that's all it takes!")
}

JSON output to a file with field parsing

package main

import (
	"log"
	"os"
	"time"

	"github.com/comail/colog"
)

func main() {
	file, err := os.OpenFile("temp_json.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
	if err != nil {
		panic(err)
	}

	colog.Register()
	colog.SetOutput(file)
	colog.ParseFields(true)
	colog.SetFormatter(&colog.JSONFormatter{
		TimeFormat: time.RFC3339,
		Flag:       log.Lshortfile,
	})

	log.Print("info: logging this to json")
	log.Print("warning: with fields foo=bar")
}

// cat tempjson.log
// {"level":"info","time":"2015-08-16T13:26:07+02:00","file":"json_example.go","line":24,"message":"logging this to json"}
// {"level":"warning","time":"2015-08-16T13:26:07+02:00","file":"json_example.go","line":25,"message":"with fields","fields":{"foo":"bar"}}

Standalone logger with level control and fixed values

package main

import (
	"log"
	"os"

	"github.com/comail/colog"
)

func main() {
	cl := colog.NewCoLog(os.Stdout, "worker ", log.LstdFlags)
	cl.SetMinLevel(colog.LInfo)
	cl.SetDefaultLevel(colog.LWarning)
	cl.FixedValue("worker_id", 42)

	logger := cl.NewLogger()
	logger.Print("this gets warning level")
	logger.Print("debug: this won't be displayed")
}

// [  warn ] worker 2015/08/16 13:43:06 this gets warning level    worker_id=42

Adding custom hooks

package main

import (
	"fmt"
	"log"

	"github.com/comail/colog"
)

type myHook struct {
	levels []colog.Level
}

func (h *myHook) Levels() []colog.Level {
	return h.levels
}

func (h *myHook) Fire(e *colog.Entry) error {
	fmt.Printf("We got an entry: \n%#v", e)
	return nil
}

func main() {
	colog.Register()
	colog.ParseFields(true)

	hook := &myHook{
		levels: []colog.Level{
			colog.LInfo,    // the hook only receives
			colog.LWarning, // these levels
		},
	}

	colog.AddHook(hook)

	colog.SetMinLevel(colog.LError) // this affects only the output
	log.Print("info: something foo=bar")
}

// We got an entry:
// &colog.Entry{Level:0x3, Time:time.Time{sec:63575323196, nsec:244349216, loc:(*time.Location)(0x23f8c0)}, Host:"",
// Prefix:"", File:"/data/workspace/comail/comail/src/comail.io/go/colog/examples/hook_example.go", Line:37,
// Message:[]uint8{0x73, 0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69, 0x6e, 0x67}, Fields:colog.Fields{"foo":"bar"}}%
Comments
  • question about best usage when using for testing

    question about best usage when using for testing

    Does it parse errors on Tests ?

    , i was hoping that doing a t.Error("alert: blah"), would display in Red. In the below example, i end up having to do a t.Error & a log.Println(alert), in order to get the RED visual warning.

    I know this is picky ! But when you have lots of tests, looking at your Bash Terminal can get really slow trying to hunt for it. The visual aids are great.

    Any advice welcome, as you can see what i can getting at.

    func init() {
    
        colog.Register()
        colog.ParseFields(true)
        colog.SetMinLevel(colog.LWarning)
        colog.SetDefaultLevel(colog.LInfo)
    }
    
    func TestGetProduct(t *testing.T) {
        t.Error("alert: thrown from t.Errror !!")
        log.Println("alert: thrown from log.Print !!")
    
    
    }
    
    

    Bash Terminal has:

    [ alert ] 2016/04/15 18:58:35 thrown from log.Print !!                                          
    --- FAIL: TestGetProducts_CategoryOnly (0.01s)
        products_test.go:105: alert: thrown from t.Errro !!
        products_test.go:112: Passed
    
    
    opened by joeblew99 2
  • Using `SetFormatter` requires `SetOutput` to print color

    Using `SetFormatter` requires `SetOutput` to print color

    prints no color:

    colog.SetFormatter(&colog.StdFormatter{})
    log.Print("warn: foobar")
    

    prints with color:

    colog.SetFormatter(&colog.StdFormatter{})
    colog.SetOutput(os.Stderr)
    log.Print("warn: foobar")
    
    opened by kyoh86 1
  • fix README.md

    fix README.md

    I change example code import "comail.io/go/colog" -> import "github.com/comail/colog". And delete trailing whitespace.

    I want to change CoLog showcase image too. But I can not.

    opened by kmtr 1
  • support colors on windows

    support colors on windows

    Following up #1 color should be supported on windows. Since colog shouldn't bring any dependencies this needs to happen via a subpackage or more likely via an extra package like cologrus

    this package could do something like have a Register() function itself and call directly colog.Register() then set colorable.NewColorableStderr() as the output.

    The question is how the colorSupported() function should become smarter to handle all this scenarios:

    • windows/no-windows OS
    • colorable/no-colorable output
    • tty/no-tty output
    // figure if output supports color
    func (cl *CoLog) colorSupported() bool {
        if runtime.GOOS == "windows" {
            return false
        }
    
        if cl.out == os.Stderr && isTerminal(int(os.Stderr.Fd())) {
            return true
        }
    
        if cl.out == os.Stdout && isTerminal(int(os.Stdout.Fd())) {
            return true
        }
    
        return false
    }
    
    opened by bictorman 0
  • Consider merging with logutils, logfilter

    Consider merging with logutils, logfilter

    I found out about colog via https://github.com/hashicorp/logutils/issues/5 Another related project is https://github.com/d2g/logfilter

    Would you be interested in joining forces? What would you need to see to deprecate colog in favor of something else? Feature parity, migration docs, perf tests, &c? Would you accept changes to enable something else to be deprecated in favor of colog?

    opened by josephholsten 0
Owner
null
Leveled execution logs for Go.

glog Leveled execution logs for Go. This is an efficient pure Go implementation of leveled logs in the manner of the open source C++ package glog. By

null 0 Nov 29, 2021
Log-server - Implement log server for gwaylib/log/adapter/rmsq

Implement server of github.com/gwaylib/log Base on https://github.com/gwaycc/lserver Build . env.sh cd cmd/web go build Deploy Install supd(Debian sy

null 0 Jan 3, 2022
Blazing fast, structured, leveled logging in Go.

⚡ zap Blazing fast, structured, leveled logging in Go. Installation go get -u go.uber.org/zap Note that zap only supports the two most recent minor ve

Uber Go 17.8k Jan 7, 2023
Hierarchical, leveled, and structured logging library for Go

spacelog Please see http://godoc.org/github.com/spacemonkeygo/spacelog for info License Copyright (C) 2014 Space Monkey, Inc. Licensed under the Apach

Space Monkey Go 98 Apr 27, 2021
Simple, customizable, leveled and efficient logging in Go

log Simple, customizable, leveled and efficient logging in Go Installation go get -u github.com/ermanimer/log Features log is a simple logging package

Erman İmer 21 Dec 20, 2021
An golang log lib, supports tracking and level, wrap by standard log lib

Logex An golang log lib, supports tracing and level, wrap by standard log lib How To Get shell go get gopkg.in/logex.v1 source code import "gopkg.in/

chzyer 40 Nov 27, 2022
Nginx-Log-Analyzer is a lightweight (simplistic) log analyzer for Nginx.

Nginx-Log-Analyzer is a lightweight (simplistic) log analyzer, used to analyze Nginx access logs for myself.

Mao Mao 28 Nov 29, 2022
Distributed-Log-Service - Distributed Log Service With Golang

Distributed Log Service This project is essentially a result of my attempt to un

Hamza Yusuff 6 Jun 1, 2022
Log-analyzer - Log analyzer with golang

Log Analyzer what do we have here? Objective Installation and Running Applicatio

Lawrence Agbani 0 Jan 27, 2022
Multi-level logger based on go std log

mlog the mlog is multi-level logger based on go std log. It is: Simple Easy to use NOTHING ELSE package main import ( log "github.com/ccpaging/lo

null 0 May 18, 2022
a golang log lib supports level and multi handlers

go-log a golang log lib supports level and multi handlers Use import "github.com/siddontang/go-log/log" //log with different level log.Info("hello wo

siddontang 33 Dec 29, 2022
Structured log interface

Structured log interface Package log provides the separation of the logging interface from its implementation and decouples the logger backend from yo

teris.io 26 Sep 26, 2022
lumberjack is a log rolling package for Go

lumberjack Lumberjack is a Go package for writing logs to rolling files. Package lumberjack provides a rolling logger. Note that this is v2.0 of lumbe

Nate Finch 3.7k Jan 1, 2023
OpenTelemetry log collection library

opentelemetry-log-collection Status This project was originally developed by observIQ under the name Stanza. It has been contributed to the OpenTeleme

OpenTelemetry - CNCF 90 Sep 15, 2022
A simple web service for storing text log files

logpaste A minimalist web service for uploading and sharing log files. Run locally go run main.go Run in local Docker container The Docker container a

Michael Lynch 266 Dec 30, 2022
exo: a process manager & log viewer for dev

exo: a process manager & log viewer for dev exo- prefix – external; from outside. Features Procfile compatible process manager.

Deref 365 Dec 28, 2022
Write log entries, get X-Ray traces.

logtoxray Write to logs, get X-Ray traces. No distributed tracing instrumenation library required. ?? ?? ?? THIS PROJECT IS A WORK-IN-PROGRESS PROTOTY

JBD 27 Apr 24, 2022
Binalyze logger is an easily customizable wrapper for logrus with log rotation

logger logger is an easily customizable wrapper for logrus with log rotation Usage There is only one function to initialize logger. logger.Init() When

Binalyze 27 Oct 2, 2022
Log-structured virtual disk in Ceph

lsd_ceph Log-structured virtual disk in Ceph 1. Vision and Goals of the Project Implement the basic librbd API to work with the research block device

null 3 Dec 13, 2021