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

Overview

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

Issues
  • 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.

    enhancement type/feature 
    opened by alessandroargentieri 2
  • ✨ 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

    type/feature 
    opened by ilmanzo 2
  • ✨ 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

    type/feature 
    opened by marcosansoni 1
  • ✨ 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

    type/feature 
    opened by davideimola 1
  • 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.

    bug 
    opened by davideimola 1
  • 📖 Change the description of Bug Report

    📖 Change the description of Bug Report

    What this PR does / why we need it:

    Change the description of the Bug Report

    type/doc 
    opened by davideimola 1
  • 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

    opened by ilmanzo 1
  • Add badges to readme

    Add badges to readme

    We want to have badges in the README.md file through the github actions. Some interesting badges could be:

    • build
    • test coverage
    • static code analysis analysis
    help wanted 
    opened by alessandroargentieri 0
  • Add the possibility to define a custom RGB color

    Add the possibility to define a custom RGB color

    Beyond the colors defined in the color.go file, create a logic to define a custom RGB color for a specific log level.

    enhancement 
    opened by alessandroargentieri 0
  • Add automatic changelog to the project

    Add automatic changelog to the project

    Add the automatic changelog to the project.

    enhancement 
    opened by davideimola 0
  • 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.

    enhancement type/feature 
    opened by alessandroargentieri 0
  • Increase Code Coverage for Colors

    Increase Code Coverage for Colors

    User Story

    As a developerI want to cover more test cases for colors in order to increase code coverage.

    opened by alessandroargentieri 0
  • 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().
    enhancement type/feature 
    opened by alessandroargentieri 0
  • 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.

    enhancement good first issue type/feature 
    opened by alessandroargentieri 0
  • 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.

    documentation help wanted 
    opened by alessandroargentieri 2
  • 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)

    enhancement 
    opened by alessandroargentieri 3
  • 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/

    enhancement good first issue type/feature 
    opened by alessandroargentieri 0
  • extract ObscureSensitiveParams function

    extract ObscureSensitiveParams function

    Now you can enable or disable the ObscureSensitiveData functionality whose check is applied to all log records when enabled. It slows down the execution time even when you log a simple record. Extract and make ObscureSensitiveParam public, in order to give the possibility to choose to disable the functionality and using it only when needed. This could be done this way:

    log.SetConfig{ &log.Configs{
             ObscureSensitiveData: log.Disable,
             SensitiveParams: []string{"password"},
         }
    }
    

    And then:

    log.Info(log.ObscureSensitiveData(myrecord))
    

    This way the feature remains disabled by default but used only when needed.

    enhancement 
    opened by alessandroargentieri 0
  • Extend sensitive data obfuscation

    Extend sensitive data obfuscation

    The regex used to obfuscate sensitive data nowadays works only on string parameters. Extend the regex or add a new regex in order to get also data without double-quotes. In that case, try to figure out if null is a better value for boolean, numbers in place of "**************" used for strings.

    enhancement good first issue 
    opened by alessandroargentieri 0
Owner
Gyoza Tech
Gyoza Tech stuff
Gyoza Tech
Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.

Noodlog Summary Noodlog is a Golang JSON parametrized and highly configurable logging library. It allows you to: print go structs as JSON messages; pr

Gyoza Tech 23 Apr 28, 2021
Zero Allocation JSON Logger

Zero Allocation JSON Logger The zerolog package provides a fast and simple logger dedicated to JSON output. Zerolog's API is designed to provide both

Olivier Poitrey 5k Jul 23, 2021
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

Bob Ziuchkovski 26 Sep 16, 2019
Structured, pluggable logging for Go.

Logrus Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger. Logrus is in maintenance-mode. We wi

Simon Eskildsen 18.3k Jul 24, 2021
Logrus is a structured, pluggable logging for Go.

Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.

Simon Eskildsen 274 May 25, 2021
Logur is an opinionated collection of logging best practices

Logur is an opinionated collection of logging best practices. Table of Contents Preface Features Installation Usage FAQ Why not just X logger? Why not

Logur 129 Jul 16, 2021
Golang logging library

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

Örjan Fors 1.7k Jul 17, 2021
A simple logging interface for Go

A more minimal logging API for Go Before you consider this package, please read this blog post by the inimitable Dave Cheney. I really appreciate what

null 430 Jul 23, 2021
Dead simple, super fast, zero allocation and modular logger for Golang

Onelog Onelog is a dead simple but very efficient JSON logger. It is one of the fastest JSON logger out there. Also, it is one of the logger with the

Francois Parquet 396 Jul 16, 2021
Structured Logging Made Easy

Structured Logging Made Easy Features Dependency Free Simple and Clean Interface Consistent Writer IOWriter, io.Writer wrapper FileWriter, rotating &

phuslu 386 Jul 22, 2021
A minimal and extensible structured logger

⚠️ PRE-RELEASE ⚠️ DO NOT IMPORT THIS MODULE YOUR PROJECT WILL BREAK package log package log provides a minimal interface for structured logging in ser

Go kit 54 Jul 21, 2021
Logging, distilled

What is distillog? distillog aims to offer a minimalistic logging interface that also supports log levels. It takes the stdlib API and only slightly e

Akshay Moghe 26 Jun 21, 2021
Fully asynchronous, structured, pluggable logging for Go.

logr Logr is a fully asynchronous, contextual logger for Go. It is very much inspired by Logrus but addresses two issues: Logr is fully asynchronous,

Mattermost 10 Jun 16, 2021
Simple, configurable and scalable Structured Logging for Go.

log Log is a simple, highly configurable, Structured Logging library Why another logging library? There's allot of great stuff out there, but also tho

Go Playgound 275 May 6, 2021