Giter Club home page Giter Club logo

abstractlogger's Introduction

abstractlogger

Abstractlogger is a logging frontend to abstract away logging from your frontend of choice.

Abstractlogger enables you to use your logging backend of choice. Currently there's a zap and logrus implementation. Feel free to add additional logging backend implementations.

You should consider using abstract logger in two situations:

  1. You're building a library and don't want to make the choice for your user on which logging backend to use.
  2. You're building an application and want logging in the hot path. You're unsure which logging library to use. In this case Abstractlogger helps you to keep your domain logic separated from a 3rd party logging library as you can change it every time without updating all of your code.

If you feel an important logging method is missing please file an issue or create a PR. Tested code is welcomed.

Benchmarks

BenchmarkNoopLogger/with_interface/log_level_invalid/noop-16    	15396679	        84.4 ns/op	     448 B/op	       2 allocs/op
BenchmarkNoopLogger/with_interface/log_level_invalid/logrus-16  	 2463068	       486 ns/op	    1938 B/op	      14 allocs/op
BenchmarkNoopLogger/with_interface/log_level_invalid/zap-16     	13834630	        85.3 ns/op	     352 B/op	       2 allocs/op
BenchmarkNoopLogger/with_interface/log_level_invalid/abstract_zap-16         	50709734	        24.4 ns/op	      96 B/op	       1 allocs/op
BenchmarkNoopLogger/with_interface/log_level_invalid/abstract_logrus-16      	48983234	        23.2 ns/op	      96 B/op	       1 allocs/op
BenchmarkNoopLogger/with_interface/log_level_valid/noop-16                   	14264962	        81.6 ns/op	     448 B/op	       2 allocs/op
BenchmarkNoopLogger/with_interface/log_level_valid/logrus-16                 	  158092	      6919 ns/op	    3401 B/op	      44 allocs/op
BenchmarkNoopLogger/with_interface/log_level_valid/zap-16                    	 4579869	       277 ns/op	     434 B/op	       3 allocs/op
BenchmarkNoopLogger/with_interface/log_level_valid/abstract_zap-16           	 3741235	       325 ns/op	     434 B/op	       3 allocs/op
BenchmarkNoopLogger/with_interface/log_level_valid/abstract_logrus-16        	  157494	      6822 ns/op	    2433 B/op	      40 allocs/op
BenchmarkNoopLogger/without_interface/log_level_invalid/noop-16              	18548176	        62.5 ns/op	     288 B/op	       1 allocs/op
BenchmarkNoopLogger/without_interface/log_level_invalid/logrus-16            	 3592113	       380 ns/op	    1393 B/op	      10 allocs/op
BenchmarkNoopLogger/without_interface/log_level_invalid/zap-16               	23941255	        48.3 ns/op	     192 B/op	       1 allocs/op
BenchmarkNoopLogger/without_interface/log_level_invalid/abstract_zap-16      	250681747	         5.14 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoopLogger/without_interface/log_level_invalid/abstract_logrus-16   	238591336	         4.96 ns/op	       0 B/op	       0 allocs/op
BenchmarkNoopLogger/without_interface/log_level_valid/noop-16                	16967037	        67.3 ns/op	     288 B/op	       1 allocs/op
BenchmarkNoopLogger/without_interface/log_level_valid/logrus-16              	  180975	      6104 ns/op	    2742 B/op	      38 allocs/op
BenchmarkNoopLogger/without_interface/log_level_valid/zap-16                 	10197316	       126 ns/op	     192 B/op	       1 allocs/op
BenchmarkNoopLogger/without_interface/log_level_valid/abstract_zap-16        	 8566719	       145 ns/op	     192 B/op	       1 allocs/op
BenchmarkNoopLogger/without_interface/log_level_valid/abstract_logrus-16     	  178683	      5900 ns/op	    2224 B/op	      37 allocs/op

The library improves the performance for non valid log levels by ~10x (zap) and ~76x (logrus). For valid log levels the overhead is minimal: No additional allocations, 13% increase (145ns vs. 126ns) for zap without using Any.

abstractlogger's People

Contributors

jensneuse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

davifrag

abstractlogger's Issues

Field struct members should be public

I need to implement a custom logger, so I'm trying to follow the examples shown for zap and logrus. Unfortunately, I'm stuck when getting to the fields function:

abstractlogger/logrus.go

Lines 28 to 49 in 2f170c5

func (l *LogrusLogger) fields(fields []Field) logrus.Fields {
out := make(logrus.Fields, len(fields))
for i := range fields {
switch fields[i].kind {
case StringField:
out[fields[i].key] = fields[i].stringValue
case ByteStringField:
out[fields[i].key] = string(fields[i].byteValue)
case IntField:
out[fields[i].key] = fields[i].intValue
case BoolField:
out[fields[i].key] = fields[i].intValue != 0
case ErrorField, NamedErrorField:
out[fields[i].key] = fields[i].errorValue
case StringsField:
out[fields[i].key] = fields[i].stringsValue
default:
out[fields[i].key] = fields[i].interfaceValue
}
}
return out
}

Because the fields on the Field object such as key, stringValue, etc. are all private:

abstractlogger/field.go

Lines 3 to 12 in 2f170c5

type Field struct {
kind FieldKind
key string
stringValue string
stringsValue []string
intValue int64
byteValue []byte
interfaceValue interface{}
errorValue error
}

They should be public so one can implement their own logger without having to resort to reflection.

Thanks.

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.