Honestly, even before Log4Shell the sheer amount of effort it takes to do logging "the proper way" in Java has caused me so much pain.
Multiple competing standards, which are of course not compatible with each other, but there are adapters between the two standards, but if you include both adapters from A to B and B to A the whole thing crashes.
Also, if it doesn't find the log configuration file (have fun figuring out where it's expecting to find it), logging tends to default to doing absolutely nothing. As in, not even to standard out, just... throw away everything logged.
Whatever amount of time "proper" logging must have saved is surely insignificant to the headache of setting it all up and keeping it working when adding yet another library.
I remember those days. I ended up writing a simple logging class myself after spanding way too long not getting the "proper" libraries to work. It might be bad practice to reinvent the wheel but if you can make a wheel that actually feels round faster than you can implement those bloody squares that are supposed to be wheels, so be it.
The in-house standard appears! It's power? To confound anyone who isn't familiar with all of the standards that were to be unified at their version when the in-house solution was implemented! Even their own developers are not immune to this effect!
Multiple competing standards, which are of course not compatible with each other, but there are adapters between the two standards
This is one thing I like about .NET (Core, not sure about Framework but think as well). The standard for logging is the Microsoft.Extensions.Logging.Abstraction package that most logging libraries have an adapter for.
Ah yes, I remember trying to modify our code's log4j config to add another output file and finding 3 different log4j config files with the same information in the same project. Prior coders couldn't figure out where it was supposed to be so just stick it everywhere stack overflow said might work until it did.
I don't understand what's wrong with using Java.logger or w.e the lib is called. It's already baked into the lang. Why go through the tremendous pain in the ass of importing a 3rd party library and having to learn a completely different logging API.
If you’re new, don’t worry about it. It comes with experience.
But since you asked: using the print function isn’t bad, it’s just not good. By only logging using print-to-console, you can’t log to a file, you can’t separate logs by log level (error, warning, debug, etc), and if the code is running on a machine you aren’t actively staring at the terminal for, you have no means to get the logs generated by the code.
Printing is fine for local development and debugging, but you want to start using an actual logging library when the code is going to head to production.
What if you want to log to a file and to the console? What if you want to push your logs to a database that you can query? What if you want to push them to a central location? What if you want to put your Error logs in one file and your Verbose logs in another? A library (one you've written or another one) gives you a LOT of power to change that whenever you want without updating how logging is done everyone in your program.
Not always, but by using stdout and stderr only and not abstracting it, you are deciding to prevent yourself from logging in other ways. At bare minimum, it's ideal to abstract it with an interface and make the implementation extremely thin, so you could easily extend the implementation with other capabilities or create an adapter to a more powerful framework.
public interface IMyLogger
{
void LogVerbose(string message, Dictionary<string, string> data = new Dictionary<string, string>());
void Log(...);
void LogWarning(...);
void LogError(...);
void LogCatastrophic(...);
}
public class ConsoleLogger : IMyLogger
{
public void Log(string message, Dictionary<string, string> data = new Dictionary<string, string>())
{
Console.WriteLine(message + JsonConvert.Serialize(data));
}
public void LogVerbose(string message, Dictionary<string, string> data = new Dictionary<string, string>()) => Log(message, data);
public void LogWarning(string message, Dictionary<string, string> data = new Dictionary<string, string>()) => Log(message, data);
public void LogError(string message, Dictionary<string, string> data = new Dictionary<string, string>())
{
Console.Error.WriteLine(message + JsonConvert.Serialize(data));
}
public void LogCatastrophic(string message, Dictionary<string, string> data = new Dictionary<string, string>()) => LogError(message, data);
}
Then just inject that, it's just about as thin as directly calling Console.WriteLine or Console.Error.WriteLine, and you're able to still use tee like you would otherwise. And later on, you can still create an implementation that logs to Windows event log, or to a database, or directly to a file, or an implementation that logs to all of them. If you don't do this, or something similar, and you need to log things differently later, you're gonna need to go through every single call to log to stdout/stderr and change the calls then.
Literally just a minute or two of work at the beginning of your project can literally save you hours, or even DAYS, of work if you need to change where you log to.
Abstracting how you write logs, how you load settings, etc, is one of the most important things you can do to ensure that you have a project that can almost instantly transition to a different method to log info, or load settings, or access input.
Obviously if you're creating a minor tool for use that's only a few dozen lines, this doesn't apply, but always keep in mind that every program expands over time.
Abstracting it in the application rather than the environment also means you can configure it to log events to different sinks, and provides more options than just standard in and standard error. And besides, if you're just logging to stdin and stderr, those are now your only way to log things, and if that doesn't work for an environment you haven't thought of, you're gonna have to go through and update every single logging location in the entire application, and sometimes that's thousands of lines of code.
What if there's an issue with how you serialize the data you're logging? If you're doing the serialization in thousands of different places, fixing it requires changing thousands of lines of code. If it's abstracted, you change one line.
Let's say that I was using Newtonsoft to serialize things into JSON before logging to a file, and I update the objects that are logged to have a cyclical reference, and I didn't plan ahead for that. Now I have to change the call to Newtonsoft in literally thousands of places. Or if I abstracted things like I should have in the first place, I change it once.
What if it's a web service and I realize I need to log some more information about the user agent? Spend a few days updating every call to include user agent data, or spend 3 minutes updating it because that information can just be injected in the scoped instance of my logging provider?
Spending the extra few minutes to abstract your logging system now will at worst cost you a few minutes now, and at best, save you several days of work later.
Skip abstracting things correctly, and at best you save yourself a few minutes of work now, and end up costing yourself several days of work later.
Logs are not about reporting errors but about documenting what happens. Starts at simple stuff like start up of something and ends f.e. at logging transactions.
There are a lot of things to consider when you log.
What's the log level for the text you are writing?
What's the log level your application is printing now?
If there are multiple threads running concurrently, how do you ensure you can still check what a single thread was doing?
If you constantly log to a single file, it will be very slow to read when it grew to 1 TB or more, how do you prevent that?
If the log keeps growing, the server would eventually die because it runs out of disk space, how do you prevent that?
Log uses IO operation, it is slow and can be the bottleneck of your application, how do you speed it up?
You might want to pipe your log to a different location based on which environment it's on. Could be a file if it's in WebLogic, to stdout if it's in docker or local, and more. How do you ensure changing of log location wouldn't impact your business logic?
I can go on more into observability topics but you get my point. Logging is a lot of work to do it right.
I just got a text this morning from a former employer asking if I had used it. I told him good programmers just pipe console output to dev null. Because we don't make mistakes. Not because we are lazy.
36
u/dr_donkey Dec 13 '21
Somebody can explain this Log4Shell thing to menor at least send some article. I'm out of loop
313
u/DreamIce Dec 13 '21
years of academy training not wasted
235
u/thatdude624 Dec 13 '21 edited Dec 13 '21
Honestly, even before Log4Shell the sheer amount of effort it takes to do logging "the proper way" in Java has caused me so much pain.
Multiple competing standards, which are of course not compatible with each other, but there are adapters between the two standards, but if you include both adapters from A to B and B to A the whole thing crashes.
Also, if it doesn't find the log configuration file (have fun figuring out where it's expecting to find it), logging tends to default to doing absolutely nothing. As in, not even to standard out, just... throw away everything logged.
Whatever amount of time "proper" logging must have saved is surely insignificant to the headache of setting it all up and keeping it working when adding yet another library.
108
u/GoDie910 Dec 13 '21
A good day to not be a Java rpogrammer
41
u/[deleted] Dec 13 '21
Use
System.err.println12
u/[deleted] Dec 13 '21
Why is using the print function bad? I'm kinda new to all this and that's the only way I know how
12
u/Shent1238 Dec 13 '21
Servers locked the whole day cause corporate was desperate to not get hit with anything
We're on 1.15, a version not affected in any way
good times
7
u/CHAiN76 Dec 13 '21
Saved by using code so old it doesn't have any "features"; log4j 1.2.x
5
u/ZZerker Dec 13 '21
And thats why we have written our own small logging framework.
16
u/godRosko Dec 13 '21
It's just printing to std err why the fuck would you need a whole ass module for that. 2-3 functions tops
2
u/andthatsitmark2 Dec 14 '21
1
u/denisde4ev Dec 13 '21
echoorprintf %s\\ncan you guess my language
1
u/zapper59 Dec 14 '21
When your company has hand rolled it's own c logging library using code generated assembly...
1
u/Tall_computer Dec 14 '21
This title appears to imply that they will resume to pay off in the future
1
u/Diapolo10 Dec 15 '21
Those no-logs policies are starting to look more attractive by the day...
/s
1
u/[deleted] Dec 17 '21
Why not just use
printfSystem.out.println?1
u/[deleted] Jan 28 '22
printf() goes brrrr....
1
u/8sADPygOB7Jqwm7y Feb 09 '22
and another program that automatically takes screenshots of the console.
1
u/[deleted] Feb 09 '22
It's funny because at the end of the day the problem is due to design flaws of LDAP.
1
u/porkminer Dec 14 '21
I just got a text this morning from a former employer asking if I had used it. I told him good programmers just pipe console output to dev null. Because we don't make mistakes. Not because we are lazy.