by linkdd · 4y ago · 0 comments
I've always done this, never have I used a library for this, because:
- running manually? >myapp.log 2>&1
- using systemd? use journalctl
- using docker/kubernetes? capture automatically the stdout/stderr of your containers and pipe them through logstash or something
Real question: why would an application need to know where its logs go? This is not in the business perimeter, but the ops perimeter. Am I wrong?EDIT:
Most responses are about configuring logging level, output format, etc... This can be done with a print-based approach without sending a request to an external service (which would need to trust you and your inputs).
Example: Python's default logging module.
Still, thank you for your insightful answers :)
log4j allows libraries to implement logging and allow the end user to worry about where the logs go, at the application level, usually via config on the command line (e.g. modifying classpaths).
In the Java world before cloud-native we had application servers like Tomcat and JBoss which typically used something like log4j (log4j-core package) to configure what gets logged and where the logs should be shipped to. These application servers where shared and usually hosted more than 1 application. Hence the need to do contextual lookups (jndi and others) to determine log routing or enrich log messages with contextual information.
Applications or libraries running inside these application servers would only implement a logging api (log4j-api package) which is just a facade so every application can write implementation-independant logging and let the application server provide the implementation and configuration.
Now in the cloud-native/12-factor world, the industry has moved away from application servers (or embeds an app-specific server in the container) so it makes sense to just log to stdout and let the container orchestration handle log shipping and filtering. The former role of log4j is now mostly handled by Fluentd, Logstash, Filebeat and friends.
Log4j still has a place though: Having the ability to use ENV vars or commandline flags to influence log levels and packages is still a nice to have. And one feature that is often overlooked is the ability to set contextual information per request. For example authentication middleware can set a userId or requestId in the request context and every log message within the request handling automatically gets logged with that metadata, avoiding the need to pass all this contextual information manually in every log.info(...) statement.
I think that an opaque library in terms of logging is absolutely hell to deal with, I already have my fair share of issues with libraries that don't do enough logging and require painful hours of debugging instead of reading a log...
So, I kick the log level from warn to debug and I see that Spring is booting it because it failed a CORS check. This was much easier than trying to reproduce it in my local system and attach a debugger to it.
There is a lot that goes on beneath and behind the code that a person writes within the libraries. I would contend that the log4j vulnerability is because people didn't realize the extent of what was going on beneath and behind.
Making a developer blind to what goes on inside a library is not the answer. The "what goes on inside of a library" is indeed a concern for the application and the developer and should be made as easy to access as possible.
Also legacy; before systemd/docker/kubernetes, applications had to send to syslog themselves.
But, depending on what your program is doing, printf/whatever can be "good enough."
Coming from the C# world, one of the nice things about log4net is that it has a standard exception formatter, and callbacks whenever anything logs to error. This makes it easier to log unexpected errors and phone home when they happen.
> This is not in the business perimeter, but the ops perimeter.
That is a great point. Logging configuration should be provided at runtime, not compiled into the application. That's why most places provide the logging configuration as a runtime parameter.
I don't understand how people would use a library as massive as log4j and not bother looking at what's in the box, and what they need to enable/disable. Blaming it on the opensource product seems weird when you grabbed it for free and didn't even read what's written on the box.
Text, though, is a pretty universal interface and I would rather wrap a binary and stream it’s output than integrate a logging library natively.
I did work with a product once that had local logs in a ring buffer and only shipped “important” logs to centralised storage, and that was nice but required context which can’t be easily gleaned from the content of a message.
Just one example.
Compare this to system.err.println("DEBUG: " + var1 + " " + var2 + " " + someThrowable.stackTrace()) -- that creates a StringBuilder, allocates a potentially large String, and then sends it on.
There are numerous performance impacts (improvements) at using a logging framework that doesn't log or allocate a String to log what it doesn't need to.
logstash is also vulnerable to this though
$ ./myapp &>myapp.log
# Get error messages mentioning "foobar"
$ jq -c 'select(.severity=="error")' | grep "foobar" | jq -r '.message'You are not a real Java Programmer then.
Imagine being able to enjoy the Java syntax without feeling the need to write everything with a minimum of three levels of abstraction!
Break free of the tyranny of getters and setters! Make that integer public!
No more oppression from type introspection! Hang Spring and Hibernate from the gallows!
The Java revolution starts TODAY!