rifflock / lfshook Goto Github PK
View Code? Open in Web Editor NEWA local file system hook for golang logrus logger
License: MIT License
A local file system hook for golang logrus logger
License: MIT License
I am using below code to log. Although the console output is JSON formatted but the log files created have text format. Can you point out what am I missing here?
`
logwriter, err := rotatelogs.New(path+"go.log.%Y%m%d%H%M",
rotatelogs.WithLinkName("goUtilitySectionLogs.current"),
rotatelogs.WithRotationTime(time.Hour),
rotatelogs.WithMaxAge(724time.Hour),
)
if err != nil {
fmt.Println("Error in initializing logging service - GoUtilitySectionLogs ", err)
}
// Log as JSON instead of the default ASCII formatter.
log.Formatter = new(logrus.JSONFormatter)
// Output to stderr instead of stdout, could also be a file.
log.Out = os.Stdout
// Only log the warning severity or above.
log.Level = logrus.InfoLevel //logrus.DebugLevel
//Adding a hook to direct the console output to logwriter
log.Hooks.Add(lfshook.NewHook(lfshook.WriterMap{logrus.InfoLevel: logwriter, logrus.ErrorLevel: logwriter}))`
my logs are going to files, but, in the same time, prints on screen, how can i disable the screen print?
^_^
Hi, as discussed in the comments on #7
Put the following into a new folder as main.go
file and run dep init
in the directory to pull in the latest sirupsen/lfshook.
package main
import (
"os"
logrus "github.com/Sirupsen/logrus"
"github.com/rifflock/lfshook"
)
type json struct {
a string
b string
}
const logFile = "log.log"
func main() {
baseLogger := logrus.New()
baseLogger.Formatter = &logrus.JSONFormatter{TimestampFormat: "2006-01-02T15:04:05.000Z"}
f, err := os.Open(logFile)
if err != nil {
f, err = os.Create(logFile)
}
hook := lfshook.NewHook(lfshook.PathMap{
logrus.InfoLevel: f.Name(),
})
baseLogger.Hooks.Add(hook)
logger := baseLogger.WithFields(logrus.Fields{
"key": "value",
})
logger.Infof("test message", json{"1", "2"})
}
go build
and then execute the binary. Note that log.log does not contain JSON
:
time="2017-11-22T13:04:47Z" level=info msg="test message%!(EXTRA main.json={1 2})" key=value
Next edit Gopkg.toml
to pick up the #7 version of lfshook as follows (you can remove the sirupsen constraint):
[[constraint]]
name = "github.com/rifflock/lfshook"
version = "=1.2.0"
dep ensure
, go build
and then execute the binary. Note that the new line in log.log is JSON
:
{"key":"value","level":"info","msg":"test message%!(EXTRA main.json={1 2})","time":"2017-11-22T13:09:13.052Z"}
Hello, I'm just opening this issue to let you know, the next release of logrus (v1.2.0) will add a new trace level named Trace below Debug.
Here is the PR for reference sirupsen/logrus#844
You may want to take into account this new level.
should lfshook.go import "github.com/sirupsen/logrus" change to
"github.com/Sirupsen/logrus"
In example use Log.Formatter = new(log.JSONFormatter) , but in lfshook.go , NewHook function use txtFormatter, and at line 138 use hook.formatter.Format(entry) . Must I use SetFormatter to replace the formatter
The introduction of:
//switch to TextFormatter
formatter := entry.Logger.Formatter
entry.Logger.Formatter = txtFormatter
defer func() {
// assign back original formatter
entry.Logger.Formatter = formatter
}()
Means logging to files using the JSONFormatter is no longer possible as we are forced to use the default:
var txtFormatter = &logrus.TextFormatter{DisableColors: true}
The version tags up to version 1.3 use the v1.3
semantic, but from there on the use of the v
is removed.
This breaks compatibility with gopkg.in that we were trying to utilize now that the breaking change that came with 2.0 was implemented.
only v.1 is supported in gopkg
If there is a good reason for removing the v
then we'll have to figure out a different way to ensure versioning of our dependencies.
Hi,
we want to include this project into Debian but can't without any stated licensing. Could you please add a (free) license text?
Thanks,
Daniel
log rotation
section is out of date, since https://github.com/lestrrat/go-file-rotatelogs has been updated and exported new APIs.
For example:
rotatelogs.New("/path/to/access_log.%Y%m%d%H%M")
Does the fd close at the time of logrus.Exit()?
I see there is a defer fd.close()
in fileWrite()
which is called by the Fire()
Does it mean that for every entry passed, the file is opened and then closed?
Wouldn't it be better if the close of the files be done during logrus.Exit(0)?
Hello! I'm preparing the log files for a specific level when the application starts like this:
pathMap := MakePath(
fileLevel,
absolutePath,
logPath,
)
func MakePath(minLevel logrus.Level, fpath, logPath string) lfshook.PathMap {
pm := lfshook.PathMap{}
for _, lvl := range logrus.AllLevels {
if lvl <= minLevel {
pm[lvl] = fpath
}
pm[logrus.WarnLevel] = logPath + "/logs/teste.log"
}
return pm
}
Is it possible to change configuration for the teste.log
name dynamically?
Or the only way is to change the file after it's creation? (The file will only be created when the first log fires).
Thanks!
I read the source code, found that every time user invoke logrus to log, then logrus fires hook writing, lfshook opens file , writes message and closes it.
why not keep it opened? Is it not efficient such way?
Hi, I'm getting this panic that seems to be caused by this code in this project:
panic: runtime error: slice bounds out of range
goroutine 8 [running]:
internal/poll.(*FD).Write(0xc4201ba140, 0xc420552000, 0x7b, 0x98, 0x0, 0x0, 0x0)
/usr/local/go/src/internal/poll/fd_unix.go:219 +0x535
os.(*File).write(0xc42033e018, 0xc420552000, 0x7b, 0x98, 0x0, 0x0, 0x0)
/usr/local/go/src/os/file_unix.go:233 +0x90
os.(*File).Write(0xc42033e018, 0xc420552000, 0x7b, 0x98, 0x0, 0x0, 0x0)
/usr/local/go/src/os/file.go:140 +0x106
github.com/rifflock/lfshook.(*lfsHook).fileWrite(0xc420064200, 0xc4201ba0f0, 0x0, 0x0)
/Users/gmendel/GO/src/github.com/rifflock/lfshook/lfshook.go:150 +0x706
github.com/rifflock/lfshook.(*lfsHook).Fire(0xc420064200, 0xc4201ba0f0, 0x0, 0x0)
/Users/gmendel/GO/src/github.com/rifflock/lfshook/lfshook.go:86 +0x9d
github.com/sirupsen/logrus.LevelHooks.Fire(0xc420080c90, 0xc400000004, 0xc4201ba0f0, 0x0, 0x0)
/Users/gmendel/GO/src/github.com/sirupsen/logrus/hooks.go:28 +0xfa
github.com/sirupsen/logrus.Entry.log(0xc42008e1e0, 0xc42052c060, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, ...)
/Users/gmendel/GO/src/github.com/sirupsen/logrus/entry.go:98 +0x19d
github.com/sirupsen/logrus.(*Entry).Info(0xc4201ba0a0, 0xc42036bf90, 0x1, 0x1)
/Users/gmendel/GO/src/github.com/sirupsen/logrus/entry.go:144 +0xd9
github.ibm.com/Bluemix/cf-admin-agent/utils/logger.(*Logger).log(0x29bbcb8, 0x2, 0xc42027e104, 0xc42036bf90, 0x1, 0x1)
...
Any idea what might cause this?
Change for README.md
func NewLogger() *logrus.Logger {
if Log != nil {
return Log
}
pathMap := lfshook.PathMap{
logrus.InfoLevel: "/var/log/info.log",
logrus.ErrorLevel: "/var/log/error.log",
}
// here need new a log .
Log = logrus.New()
Log.Hooks.Add(lfshook.NewHook(
pathMap,
&logrus.JSONFormatter{},
))
return Log
}
I haven't created '/var/log/test.log' file. I got below error:
Failed to fire hook: open /var/log/test.log: permission denied
However, after creating file and assigning proper permission things worked fine.
Is this the intended behaviour ?
Currently, lfshook only logs to the filesystem using the TextFormatter. It might be useful to construct the hook while specifying which type of formatter to use, since the stdout formatter can differ.
go-file-rotatelogs api has changed
import (
"log"
"net/http"
apachelog "github.com/lestrrat/go-apache-logformat"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ... })
logf, err := rotatelogs.New(
"/path/to/access_log.%Y%m%d%H%M",
rotatelogs.WithLinkName("/path/to/access_log"),
rotatelogs.WithMaxAge(24 * time.Hour),
rotatelogs.WithRotationTime(time.Hour),
)
if err != nil {
log.Printf("failed to create rotatelogs: %s", err)
return
}
http.ListenAndServe(":8080", apachelog.Wrap(mux, logf))
}
All tags point to the same commit; so all releases are the same code, making them useless.
I'm tracking 1.7 and recent 2.0 release broke my code because 1.7 is now pointing to the same commit that was released 2.0.
Given that bd08170 was a breaking change that required major version bump, this is bad.
I'm not sure how did this happen!
Hi Rifflock, Can you show a use example of this package? I'm bit confuse to use this in global scope in my go application. Thank you!
I would like to log accodingly to field in the log, like this:
log.WithFields(log.Fields{"Field": "logname"}).Info("My log message!")
So all logs that contains that field would go to logname.log, if I have:
log.WithFields(log.Fields{"Field": "logname2"}).Info("My log message!")
It would go to logname2.log
How can I achieve that ?
case-insensitive import collision: "github.com/Sirupsen/logrus" and "github.com/sirupsen/logrus"
Windows detected
As the title..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.