Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.

Noodlog

License CodeFactor Go Report Card Maintenance made-with-Go Open Source Love svg1

alt text

Summary

Noodlog is a Golang JSON parametrized and highly configurable logging library.

It allows you to:

  • print go structs as JSON messages;
  • print JSON strings and raw strings messages as pure JSONs;
  • obscure some sensitive params from your logging;
  • chain objects or strings in your logs;
  • apply string templates to your logs;
  • choose to trace the caller file and function and fine tune the settings;
  • apply pretty printing or not;
  • apply colors to your logging;
  • customize colors per log level.

Import

go get github.com/gyozatech/noodlog

Usage

Let's assume you have Go 1.16+ istalled on your computer. Execute the following:

$ mkdir example && cd example
$ go mod init example
$ go get github.com/gyozatech/noodlog
$ touch main.go

Open main.go and paste the following code:

package main

import (
    log "github.com/gyozatech/noodlog"
)

func init() {
   log.SetConfigs(
      log.Configs{
         LogLevel: log.LevelTrace,
         JSONPrettyPrint: log.Enable,
         TraceCaller: log.Enable,
         Colors: log.Enable,
         CustomColors: &log.CustomColors{ Trace: log.Cyan },
         ObscureSensitiveData: log.Enable,
         SensitiveParams: []string{"password"},
      },
    )
}

func main() {
    // simple string message (with custom color)
    log.Trace("Hello world!")
    
    // chaining elements
    log.Info("You've reached", 3, "login attemps")
    
    // using string formatting
    log.Warn("You have %d attempts left", 2)
    
    // logging a struct with a JSON
    log.Error(struct{Code int; Error string}{500, "Generic Error"})
    
    // logging a raw JSON string with a JSON (with obscuring "password")
    log.Info(`{"username": "gyozatech", "password": "Gy0zApAssw0rd"}`)
    
    // logging a JSON string with a JSON (with obscuring "password")
    log.Info("{\"username\": \"nooduser\", \"password\": \"N0oDPasSw0rD\"}")
}

Running this example with:

$ go run main.go

You'll get the following output:

alt text

Settings

Noodlog allows you to customize the logs through various settings. You can use various facility functions or the SetConfigs function which wraps all the configs together.


LogLevel

To set the logging level, after importing the library with:

import (
    log "github.com/gyozatech/noodlog"
)

you can use the facility method:

log.LogLevel("warn")

or the SetConfigs function:

log.SetConfigs(
    log.Configs{
        LogLevel: log.LevelWarn,
    },
)

log.LevelWarn is a pre-built pointer to the string "warn".

The default log level is info.


JSON Pretty Printing

After importing the library with:

import (
    log "github.com/gyozatech/noodlog"
)

To enable pretty printing of the JSON logs you can use:

log.EnableJSONPrettyPrint()

or

log.SetConfigs(
    log.Configs{
    JSONPrettyPrint: log.Enable,
    },
)

log.Enable is a pre-built pointer to the bool true.

to disable pretty printing you can use:

log.DisableJSONPrettyPrint()

or

log.SetConfigs(
    log.Configs{
    JSONPrettyPrint: log.Disable,
    },
)

log.Disable is a pre-built pointer to the bool false.

The default value is false.


Colors

After importing the library with:

import (
    log "github.com/gyozatech/noodlog"
)

to enable colors in JSON logs you can use:

log.EnableColors()

or

log.SetConfigs(
    log.Configs{
        Colors: log.Enable,
    },
)

log.Enable is a pre-built pointer to the bool true.

To disable colors you can use:

log.DisableColors()

or

log.SetConfigs(
    log.Configs{
        Colors: log.Disable,
    },
)

log.Disable is a pre-built pointer to the bool false.

The default value is false.

You can customize the single colors (for log level) by using:

log.SetTraceColor("cyan")
log.SetDebugColor("green")
log.SetInfoColor("default")
log.SetWarnColor("yellow")
log.SetErrorColor("purple")

or

log.SetConfigs(
    log.Configs{
        Colors: log.Enable,
        CustomColors: &log.CustomColors{ 
            Trace: log.Cyan, 
            Debug: log.Green,
            Info:  log.Default,
            Warn:  log.Yellow,
            Error: log.Purple,    
        },
    },
)

log.Cyan, log.Green, log.Default, log.Yellow, log.Purple, log.Red, log.Blue are pre-build pointers to the strings "cyan", "green", "default", "yellow", "purple", "red", "blue".

When enabled, the default colors are:

  • trace: "default"
  • info: "default"
  • debug: "green"
  • warn: "yellow"
  • error: "red"

Trace the caller

Noodles allows you to print the file and the function which are calling the log functions.

After importing the library with:

import (
    log "github.com/gyozatech/noodlog"
)

to enable the trace caller you can use:

log.EnableTraceCaller()

or

log.SetConfigs(
    log.Configs{
        TraceCaller: log.Enable,
    },
)

log.Enable is a pre-built pointer to the bool true.

To disable it:

log.DisableTraceCaller()

or

log.SetConfigs(
    log.Configs{
        TraceCaller: log.Disable,
    },
)

log.Disable is a pre-built pointer to the bool false.

The default value is false.

Important: if you want to import noodlog only in one package of your project (in order to configure it once) and wraps the logging functions you can use the EnableSinglePointTracing to trace file and function the real caller and not of your logging package.

For example:

main.go

package main

import (
   log "example/logging"
)

func main() {
   // main.main real caller we want to track 
   log.Info("Hello folks!")
}

logging/logger.go

package logging

import (
    "github.com/gyozatech/noodlog"
)

func init() {
    // configure logger once
    noodlog.SetConfig(
        noodlog.Configs{
         TraceCaller: noodlog.Enable,
         SinglePointTracing: noodlog.Enable,
      },
    )
}

// wrapper function
func Info(message ...interface{}) {
    // if we wouldn't enable SinglePointTracing
    // logger.Info would have been considered the caller to be tracked
    noodlog.Info(message...)
}

Sensitive params

Noodlog gives you the possibility to enable the obscuration of sensitive params when recognized in the JSON structures (not in the simple strings that you compose).

After importing the library with:

import (
    log "github.com/gyozatech/noodlog"
)

You can enable the sensitive params obscuration with the facility methods:

log.EnableObscureSensitiveData([]string{"param1", "param2", "param3"})

or with the SetConfig function:

log.SetConfigs(
    log.Configs{
        ObscureSensitiveData: log.Enable,
        SensitiveParams: []string{"param1", "param2", "param3"},
    },
)

Where log.Enable is a pre-built pointer to the bool true.

To disable the sensitive params obscuration you can set:

log.DisableObscureSensitiveData()

or

log.SetConfigs(
    log.Configs{
        ObscureSensitiveData: log.Disable,
    },
)

Where log.Disable is a pre-built pointer to the bool false.

The default value for the obscuration is false.


Contribute to the project

To contribute to the project create a fork on your personal Github profile and open a pull request to the main branch of the project using the template specified here:

(PR Template)[https://github.com/gyozatech/noodlog/blob/main/.github/PULL_REQUEST_TEMPLATE.md]

Benchmark

Owner
Gyoza Tech
Gyoza Tech stuff
Gyoza Tech
Comments
  • Logs don't go new line

    Logs don't go new line

    Bug description

    The log messages don't go to next line.

    How to reproduce

    Just check the logged messages

    Environment

    • Go version 16+
    • OS and Architecture: any

    Anything else

    Hint: change the fmt.Fprintf() function into fmt.Fprintln() into logger.go -> printLog function.

  • 🐛 Fix for log statements not going to next line

    🐛 Fix for log statements not going to next line

    What this PR does / why we need it: Every log line that is outputted doesn't add a new line statement at the end of it. This PR attempts ti fix the issue of adding a newline character after every log line output.

    Which issue(s) this PR fixes Fixes #27

  • ✨ added func to change logWriter and started implementing some tests

    ✨ added func to change logWriter and started implementing some tests

    What this PR does / why we need it:

    • Implements a way to redirect the log stream to a file or a memory buffer (for testing)
    • improves performance by using a string Builder instead of concatenation
    • adds minimal tests in data-driven format to test the logger

    Which issue(s) this PR fixes
    Fixes #10 , #11

  • Add the possibility to print log in files

    Add the possibility to print log in files

    User Story

    As a developer, I would like to have the possibility to choose to redirect all logs in a file (one file for the day) in place of standard output.

    Detailed Description Add a setting to define filename, log rotation, etc. and switch the logic when needed.

  • 💥 Issue/24

    💥 Issue/24

    This PR transforms the logger to a struct:

    This PR fixes issue/24

    The logger now becomes a struct so the developer can instantiate a different logger with different configs in different parts of the same application. Before of this PR the logger is only a bunch of functions and global configs. This allows also us not to have some random issues when unit tests are launched in parallel.

  • Refactor logger as struct

    Refactor logger as struct

    User Story

    As a user, I would like to create an instance of my logger to prevent using a global set of functions.

    Detailed Description

    By having a logger with a set of global functions and settings we can have concurrency issues, mostly in tests.

    Create a struct Logger and move all the configs and behaviors on this object. Also, tests and documentation must be updated after this deep refactor.

  • configure logger to a known level before testing 🐛

    configure logger to a known level before testing 🐛

    What this PR does / why we need it: sets the loglevel before running the test . Be aware that since tests are run in parallel, you may still encounter other similar issues with the tests since you are using a "global" configuration.

    Fixes #21

  • Test TestSimpleLogger is flaky

    Test TestSimpleLogger is flaky

    Bug description

    The test TestSimpleLogger is a bit flaky. We must resolve the flakiness.

    How to reproduce

    Run the tests without cache and you can see the test can go both green and red.

  • ✨ Add Code Coverage with Codecov

    ✨ Add Code Coverage with Codecov

    What this PR does / why we need it: Adds the Code Coverage to this Project with Codecov.com

    Which issue(s) this PR fixes Fixes #16

  • ✨ Added rgb color support and background color

    ✨ Added rgb color support and background color

    What this PR does / why we need it:

    It introduces the RGB Support for color in logging. Furthermore, now it is possible to add a background color for a log (and customize it using a RGB notation)

    Which issue(s) this PR fixes

    Fixes #3

  • Color enhancements

    Color enhancements

    User Story

    As a developer, I would like to make some enhancements to Colors.

    Detailed Description

    The proposed enhancements are as follows:

    • move Color struct and its methods to log_struct.go file where all the structs of the library are contained. (Even if it's very contextual to have it in color.go file)
    • if Color struct and its attribute Code are only reachable from the internal of the noodlog package, change to lowercase letters (color, code). If only Code can be private, it would be the only one to be changed lowercase.
    • if we can use a default code color, change the color.code from pointer to string. This way we can avoid using the function ToCode() in order to get its string value, but we can access directly with color.code instead of color.ToCode(). If it's not possible to remove the pointer and to delete ToCode() function, evaluate if it's possible to use lowercase toCode().
  • ✨ Feature/18

    ✨ Feature/18

    This PR adds the feature to add custome time format for JSON logs

    **This PR added new feature #18 **

    With this feature users shall be able to define the timezone and time format to get the desired time in the JSON logs

  • Include examples of

    Include examples of "out of the box" logs format in the README

    Currently, in the README is possible to see an example of a colorized pretty-printed output. This is very cool, although it would be useful for someone stumbling upon this library knowing what's the default behavior without any of the configured parameters.

    Sometimes it's useful to print everything on a single line and let the use possibly format the output, and some people would look for that specific feature first

    Good stuff! :+1:

  • Enable Datetime format customization

    Enable Datetime format customization

    User Story

    As an operator, I would like to get a DateTime formatted according to my needs.

    Detailed Description

    Maintain the present formatting as default and allow user customization in order to vary the format, consider the timezone too.

  • Create Benchmark

    Create Benchmark

    User Story

    As a developer, I would like to have a benchmark for noodlog performances.

    Detailed Description

    It's also possible to compare noodlog with logrus, because it solves similar problems and has similar performances.

    Anything else

    Keep in mind that enabling sensitive data obscuration, colors, pretty printing is more cost computational than not using these confs.

    Keep also in mind that printing a JSON or an Object is more cost computational that writing a simple string record.

  • Spike: check if you can substitute encoding/json marshalling with a faster json library to remove reflection

    Spike: check if you can substitute encoding/json marshalling with a faster json library to remove reflection

    User Story

    As a user, I would like to get higher performance for logging.

    Detailed Description

    Try to substitute classic "encoding/json" marshalling/unmarshalling which uses reflection, with a faster json library (i.e. https://github.com/mailru/easyjson)

  • Add config to redirect error logs to stderr

    Add config to redirect error logs to stderr

    User Story

    As a developer, I would like to redirect Error, Fatal, Panic logs to standard error instead of standard output.

    Detailed Description

    Create optional configs to perform the routing logic

    EnableStdErr()
    

    Take inspiration from: https://www.systutorials.com/how-to-print-a-line-to-stderr-and-stdout-in-go/

With this package you can create your own syslog server with your own handlers for different kind of syslog messages

Using this library you can easy implement your own syslog server that: Can listen on multiple UDP ports and unix domain sockets. Can pass parsed syslo

Nov 9, 2022
Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs

gomol Gomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go. Gomol grew from a desire to have a structured logging libra

Sep 26, 2022
Secure logger in Go to avoid output sensitive data in log
Secure logger in Go to avoid output sensitive data in log

zlog A main distinct feature of zlog is secure logging that avoid to output secret/sensitive values to log. The feature reduce risk to store secret va

Dec 6, 2022
A simple logging module for go, with a rotating file feature and console logging.

A simple logging module for go, with a rotating file feature and console logging. Installation go get github.com/jbrodriguez/mlog Usage Sample usage W

Dec 14, 2022
FactorLog is a logging infrastructure for Go that provides numerous logging functions for whatever your style may be
FactorLog is a logging infrastructure for Go that provides numerous logging functions for whatever your style may be

FactorLog FactorLog is a fast logging infrastructure for Go that provides numerous logging functions for whatever your style may be. It could easily b

Aug 3, 2022
Package logging implements a logging infrastructure for Go
Package logging implements a logging infrastructure for Go

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Nov 10, 2021
This package enables json output, level logging and so on to standard go logger.

logplug This package enables json output, level logging and so on to standard logger. Usage log.SetOutput(logplug.NewJSONPlug(os.Stderr, logplug.LogF

Dec 27, 2021
a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library

English | 中文 olog olog is a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library. Support outpu

Apr 12, 2023
Simple and blazing fast lockfree logging library for golang
Simple and blazing fast lockfree logging library for golang

glg is simple golang logging library Requirement Go 1.11 Installation go get github.com/kpango/glg Example package main import ( "net/http" "time"

Nov 28, 2022
ChangeTower is intended to help you watch changes in webpages and get notified of any changes written in Go

ChangeTower is intended to help you watch changes in webpages and get notified of any changes written in Go

Nov 17, 2022
Logging library for Golang

GLO Logging library for Golang Inspired by Monolog for PHP, severity levels are identical Install go get github.com/lajosbencz/glo Severity levels Deb

Sep 26, 2022
Golang logging library
Golang logging library

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Dec 27, 2022
Cloud logging library in golang

?? logg Open Source Cloud logging library in Go. About the project Connect your golang microservices logs with this engine! Send your logs to kafka, r

Nov 8, 2021
The Simplest and worst logging library ever written

gologger A Simple Easy to use go logger library. Displays Colored log into console in any unix or windows platform. You can even store your logs in fi

Sep 26, 2022
Seelog is a native Go logging library that provides flexible asynchronous dispatching, filtering, and formatting.

Seelog Seelog is a powerful and easy-to-learn logging framework that provides functionality for flexible dispatching, filtering, and formatting log me

Jan 3, 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

Apr 27, 2021
A Go (golang) package providing high-performance asynchronous logging, message filtering by severity and category, and multiple message targets.

ozzo-log Other languages 简体中文 Русский Description ozzo-log is a Go package providing enhanced logging support for Go programs. It has the following fe

Dec 17, 2022
Minimalistic logging library for Go.
Minimalistic logging library for Go.

logger Minimalistic logging library for Go. Blog Post Features: Advanced output filters (package and/or level) Attributes Timers for measuring perform

Nov 16, 2022
A pure Go contextual logging library with "batteries included"

Cue Overview Cue implements contextual logging with "batteries included". It has thorough test coverage and supports logging to stdout/stderr, file, s

Sep 16, 2019