Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f2ac81d
extract error interface to a new error field
Ak-Army Mar 13, 2018
c331425
remove error field from default output
Ak-Army Mar 19, 2018
a6e0cb0
fork in a go way
Ak-Army Mar 19, 2018
176eee4
Real fork of main package, and make go.mod compatible
Ak-Army Jun 25, 2019
375e85a
add gitignore
Ak-Army Jun 25, 2019
986c435
better error output
Ak-Army Nov 6, 2019
3a895f3
remove error from message string
Ak-Army Nov 6, 2019
8aa6d94
remove error from message string
Ak-Army Nov 6, 2019
c8258e5
if just error found in the message, do not handle-d it az error type
Ak-Army Nov 7, 2019
fc530de
add setFields to set multiple fields
Ak-Army Jan 22, 2020
6d553be
update deps
Feb 11, 2021
1280582
Merge pull request #1 from vcc-mark-bartos/master
Ak-Army Feb 12, 2021
5417722
feature: get the global logger
Ak-Army Feb 17, 2021
e8a8de7
feature: ability to disable caller info from log lines
Ak-Army Jun 25, 2024
8051abc
feature: use sprintf %q to escape string instead of json marshal to s…
Ak-Army Jun 26, 2024
05e68c8
fix: color bug
Ak-Army Jun 26, 2024
bc19a28
fix: update README.md
Ak-Army Jun 26, 2024
1aec438
feature: ability to use consol output to file
Ak-Army Jun 26, 2024
ed92be5
feature: turn off colors in widows
Ak-Army Jul 4, 2024
da4848d
feature: add FromContextWithFields, add FromContextWithStd
Ak-Army Aug 29, 2024
55ad774
fix: setFields create fields if not initielaized
Ak-Army Aug 29, 2024
24494b2
feature: ability to set context for log, output can use context to ga…
Ak-Army Sep 2, 2024
5f118b7
feature: update dependencies, handle panic in send
Ak-Army Jul 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# IntelliJ
.idea

# Binaries for programs and plugins
/build

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
/vendor

10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

38 changes: 16 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
:warning: **Check [zerolog](https://github.com/rs/zerolog), the successor of xlog.**


# HTTP Handler Logger

[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/xlog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/xlog/master/LICENSE) [![Build Status](https://travis-ci.org/rs/xlog.svg?branch=master)](https://travis-ci.org/rs/xlog) [![Coverage](http://gocover.io/_badge/github.com/rs/xlog)](http://gocover.io/github.com/rs/xlog)
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/Ak-Army/xlog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/Ak-Army/xlog/master/LICENSE) [![Build Status](https://travis-ci.org/Ak-Army/xlog.svg?branch=master)](https://travis-ci.org/Ak-Army/xlog) [![Coverage](http://gocover.io/_badge/github.com/Ak-Army/xlog)](http://gocover.io/github.com/Ak-Army/xlog)

`xlog` is a logger for [net/context](https://godoc.org/golang.org/x/net/context) aware HTTP applications.

Unlike most loggers, `xlog` will never block your application because one its outputs is lagging. The log commands are connected to their outputs through a buffered channel and will prefer to discard messages if the buffer get full. All message formatting, serialization and transport happen in a dedicated go routine.

Read more about `xlog` on [Dailymotion engineering blog](http://engineering.dailymotion.com/our-way-to-go/).

![](screenshot.png)

## Features

- Per request log context
- Per request and/or per message key/value fields
- Log levels (Debug, Info, Warn, Error)
- Log levels (Debug, Info, Warn, Error, Fatal)
- Color output when terminal is detected
- Custom output (JSON, [logfmt](https://github.com/kr/logfmt), …)
- Automatic gathering of request context like User-Agent, IP etc.
- Drops message rather than blocking execution
- Easy access logging thru [github.com/rs/xaccess](https://github.com/rs/xaccess)

Works with both Go 1.7+ (with `net/context` support) and Go 1.6 if used with [github.com/rs/xhandler](https://github.com/rs/xhandler).

## Install

go get github.com/rs/xlog
go get github.com/Ak-Army/xlog

## Usage

Expand Down Expand Up @@ -158,7 +152,7 @@ xlog.SetLogger(xlog.New(xlog.Config{

By default, output is setup to output debug and info message on `STDOUT` and warning and errors to `STDERR`. You can easily change this setup.

XLog output can be customized using composable output handlers. Thanks to the [LevelOutput](https://godoc.org/github.com/rs/xlog#LevelOutput), [MultiOutput](https://godoc.org/github.com/rs/xlog#MultiOutput) and [FilterOutput](https://godoc.org/github.com/rs/xlog#FilterOutput), it is easy to route messages precisely.
XLog output can be customized using composable output handlers. Thanks to the [LevelOutput](https://godoc.org/github.com/Ak-Army/xlog#LevelOutput), [MultiOutput](https://godoc.org/github.com/Ak-Army/xlog#MultiOutput) and [FilterOutput](https://godoc.org/github.com/Ak-Army/xlog#FilterOutput), it is easy to route messages precisely.

```go
conf := xlog.Config{
Expand Down Expand Up @@ -187,25 +181,25 @@ h = xlog.NewHandler(conf)

| Name | Description |
|------|-------------|
| [OutputChannel](https://godoc.org/github.com/rs/xlog#OutputChannel) | Buffers messages before sending. This output should always be the output directly set to xlog's configuration.
| [MultiOutput](https://godoc.org/github.com/rs/xlog#MultiOutput) | Routes the same message to several outputs. If one or more outputs return error, the last error is returned.
| [FilterOutput](https://godoc.org/github.com/rs/xlog#FilterOutput) | Tests a condition on the message and forward it to the child output if true.
| [LevelOutput](https://godoc.org/github.com/rs/xlog#LevelOutput) | Routes messages per level outputs.
| [ConsoleOutput](https://godoc.org/github.com/rs/xlog#NewConsoleOutput) | Prints messages in a human readable form on the stdout with color when supported. Fallback to logfmt output if the stdout isn't a terminal.
| [JSONOutput](https://godoc.org/github.com/rs/xlog#NewJSONOutput) | Serialize messages in JSON.
| [LogfmtOutput](https://godoc.org/github.com/rs/xlog#NewLogfmtOutput) | Serialize messages using Heroku like [logfmt](https://github.com/kr/logfmt).
| [LogstashOutput](https://godoc.org/github.com/rs/xlog#NewLogstashOutput) | Serialize JSON message using Logstash 2.0 (schema v1) structured format.
| [SyslogOutput](https://godoc.org/github.com/rs/xlog#NewSyslogOutput) | Send messages to syslog.
| [UIDOutput](https://godoc.org/github.com/rs/xlog#NewUIDOutput) | Append a globally unique id to every message and forward it to the next output.
| [OutputChannel](https://godoc.org/github.com/Ak-Army/xlog#OutputChannel) | Buffers messages before sending. This output should always be the output directly set to xlog's configuration.
| [MultiOutput](https://godoc.org/github.com/Ak-Army/xlog#MultiOutput) | Routes the same message to several outputs. If one or more outputs return error, the last error is returned.
| [FilterOutput](https://godoc.org/github.com/Ak-Army/xlog#FilterOutput) | Tests a condition on the message and forward it to the child output if true.
| [LevelOutput](https://godoc.org/github.com/Ak-Army/xlog#LevelOutput) | Routes messages per level outputs.
| [ConsoleOutput](https://godoc.org/github.com/Ak-Army/xlog#NewConsoleOutput) | Prints messages in a human readable form on the stdout with color when supported. Fallback to logfmt output if the stdout isn't a terminal.
| [JSONOutput](https://godoc.org/github.com/Ak-Army/xlog#NewJSONOutput) | Serialize messages in JSON.
| [LogfmtOutput](https://godoc.org/github.com/Ak-Army/xlog#NewLogfmtOutput) | Serialize messages using Heroku like [logfmt](https://github.com/kr/logfmt).
| [LogstashOutput](https://godoc.org/github.com/Ak-Army/xlog#NewLogstashOutput) | Serialize JSON message using Logstash 2.0 (schema v1) structured format.
| [SyslogOutput](https://godoc.org/github.com/Ak-Army/xlog#NewSyslogOutput) | Send messages to syslog.
| [UIDOutput](https://godoc.org/github.com/Ak-Army/xlog#NewUIDOutput) | Append a globally unique id to every message and forward it to the next output.

## Third Party Extensions

| Project | Author | Description |
|---------|--------|-------------|
| [gRPClog](https://github.com/clawio/grpcxlog) | [Hugo González Labrador](https://github.com/labkode) | An adapter to use xlog as the logger for grpclog.
| [xlog-nsq](https://github.com/rs/xlog-nsq) | [Olivier Poitrey](https://github.com/rs) | An xlog to [NSQ](http://nsq.io) output.
| [xlog-nsq](https://github.com/Ak-Army/xlog-nsq) | [Olivier Poitrey](https://github.com/rs) | An xlog to [NSQ](http://nsq.io) output.
| [xlog-sentry](https://github.com/trong/xlog-sentry) | [trong](https://github.com/trong) | An xlog to [Sentry](https://getsentry.com/) output.

## Licenses

All source code is licensed under the [MIT License](https://raw.github.com/rs/xlog/master/LICENSE).
All source code is licensed under the [MIT License](https://raw.github.com/Ak-Army/xlog/master/LICENSE).
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/Ak-Army/xlog

go 1.21

require (
github.com/rs/xid v1.6.0
github.com/stretchr/testify v1.10.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 24 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.7
// +build go1.7

package xlog
Expand Down Expand Up @@ -44,6 +45,29 @@ func FromContext(ctx context.Context) Logger {
return l
}

// FromContextWithStd gets the logger out of the context.
// If not logger is stored in the context, a GetLogger is returned.
func FromContextWithStd(ctx context.Context) Logger {
if ctx == nil {
return GetLogger()
}
l, ok := ctx.Value(logKey).(Logger)
if !ok {
return GetLogger()
}
return l
}

// FromContextWithFields gets the logger out of the context.
// If not logger is stored in the context, a GetLogger is returned.
func FromContextWithFields(ctx context.Context, fields F) Logger {
l := FromContextWithStd(ctx)
l = Copy(l)
l.SetFields(fields)

return l
}

// FromRequest gets the logger in the request's context.
// This is a shortcut for xlog.FromContext(r.Context())
func FromRequest(r *http.Request) Logger {
Expand Down
65 changes: 0 additions & 65 deletions handler_examples_test.go

This file was deleted.

Loading