Giter VIP home page Giter VIP logo

slogger's Introduction

slogger Go Report Card

  .--.--.     ,--,                                                      
 /  /    '. ,--.'|                                                      
|  :  /`. / |  | :     ,---.                                    __  ,-. 
;  |  |--`  :  : '    '   ,'\   ,----._,.  ,----._,.          ,' ,'/ /| 
|  :  ;_    |  ' |   /   /   | /   /  ' / /   /  ' /   ,---.  '  | |' | 
 \  \    `. '  | |  .   ; ,. :|   :     ||   :     |  /     \ |  |   ,' 
  `----.   \|  | :  '   | |: :|   | .\  .|   | .\  . /    /  |'  :  /   
  __ \  \  |'  : |__'   | .; :.   ; ';  |.   ; ';  |.    ' / ||  | '    
 /  /`--'  /|  | '.'|   :    |'   .   . |'   .   . |'   ;   /|;  : |    
'--'.     / ;  :    ;\   \  /  `---`-'| | `---`-'| |'   |  / ||  , ;    
  `--'---'  |  ,   /  `----'   .'__/\_: | .'__/\_: ||   :    | ---'     
             ---`-'            |   :    : |   :    : \   \  /           
                                \   \  /   \   \  /   `----'            
                                 `--`-'     `--`-'                      

The logger library using the experimental Golang slog package

Usage:

  • The package was designed to be a simple interface over the slog package extracing out some if the discoverability features and crafting a simple, easy logger.
  • creating a new logger with NewLogger function. The input is an io.Writer, so stdout or a file work (any any io.Writer implementation)
  • All variadic values passed to the LogEvent/LogError functions will be treated as key value pairs, any additional values, that are not mapped to their preceeding values, are added as "misc" fields to the JSON return.

Example Usage:

  • create a logger with slogger.NewLogger(os.Stdout)
  • or log to a file:
f, _ := os.OpenFile(testFile, os.O_RDWR|os.O_TRUNC, os.ModeAppend)
log := slogger.NewLogger(f)
stdoutLogger.LogEvent("info", "Send data to file") 
  • slogger will accept any number of arbitrary elements and create a json structure from the arguments to the LogEvent functions.
  • i.e.
stdoutLogger.LogEvent("info", "Something", "one", "two", "Another", false, "four", true)

output:

stdoutLogger.LogEvent("info", "Something four", "one", "two", "Another", false, "four", true)
  • You can also pass a map to the LogEvent functions as unput:
stdoutLogger.LogEvent("info", "Something four", map[string]any{"one": false})

output:

{"time":"2022-12-27T19:19:22.711393515-08:00","level":"INFO","msg":"Something four","one":false}
  • You can even pass a combination of map strings:
stdoutLogger.LogEvent("warn", "aaaa", "key", "value", "AnotherKey", false, "four", 123123, map[string]any{"testOne": 42069, "testTwo": false})

output:

stdoutLogger.LogEvent("warn", "aaaa", "key", "value", "AnotherKey", false, "four", 123123, map[string]any{"testOne": 42069, "testTwo": false})
  • Example of Misc fields:
{"time":"2022-12-27T18:56:27.543409183-08:00","level":"INFO","msg":"Something four","one":"two","Another":false,"four":true,"miscFields":"bob"}

Output:

  • Example test output:
{"time":"2022-12-27T18:56:27.543409183-08:00","level":"INFO","msg":"Something four","one":"two","Another":false,"four":true,"miscFields":"bob"}
{"time":"2022-12-27T18:56:27.543564927-08:00","level":"INFO","msg":"Something four","one":"two","Another":false,"four":true}
{"time":"2022-12-27T18:56:27.543572397-08:00","level":"INFO","msg":"Something four","one":false,"Another":false,"four":true}
{"time":"2022-12-27T18:56:27.543577644-08:00","level":"INFO","msg":"A thing","key":"value","AnotherKey":false,"four":123123,"more":"","MORETEST":123123,"TestAgain":false,"miscFields":123123}
{"time":"2022-12-27T18:56:27.543583676-08:00","level":"WARN","msg":"aaaa","AnotherKey":false,"four":123123,"testOne":42069,"testTwo":false,"key":"value"}
{"time":"2022-12-27T18:56:27.543587977-08:00","level":"ERROR","msg":"error","err":"something died"}
{"time":"2022-12-27T18:56:27.54359305-08:00","level":"ERROR","msg":"error","1":2,"3":"masdasd","err":"something died"}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.