News for hackers. Stuff that matters.
Re:Good Logging by perryizgr8 on Friday July 24, 2020 @03:59AM (#23936893)
I feel that logging is a seriously hard problem and even experienced programmers struggle with it. It is almost impossible to anticipate what information will be useful later. So the first problem is to figure out the right logs at the right places.

The second problem is to figure out if logging is affecting performance. Or if a couple of logs can overpower everything else in a specific edge case. This will need thoughtful rate limiting and a good knowledge of the use cases your system will be subjected to.

Third problem is journald. Those journal logs are the worst. The viewer journalctl is probably the worst log viewing app that exists. These log files also have the unique quality of not being of any use outside the system they were generated on. No way to open them in Windows. Even on Linux, the times are all wrong. I don't know if that is a problem with journalctl or my system.

Re:Good Logging by BenoitP on Friday July 24, 2020 @04:54AM (#23937263)
> So the first problem is to figure out the right logs at the right places.

Over the years I have come to the following rule: during the development design-code-test loop, if the code does not work the first time it gets a logging line where it failed. It helps with debugging right now, and makes a sharp edge surface by some definition. You don't know what you don't know, so you might as well have information gathering around entropy-generating places.

Other than that:

* Don't log in high throughput loops (test them extra!); if you must, use a rate limiting logger with expiring delay.

* Log important parameters

* Log assumptions. Especially about data. There's always way more entropy in data.

* This applies to user-reported bugs, which is a form of a failed assumption. All information necessary to debug the issue now must go to the log.

Also end-to-end testing >> unit testing

Re:Good Logging by MaxBarraclough on Friday July 24, 2020 @05:46AM (#23937564)
> Don't log in high throughput loops

Looks deceptively simple. Typically a function implements some behaviour or computation, and hopefully does so correctly and efficiently. Its usage is, well, up to the user. With the requirement that we don't log in high-throughput loops, this is no longer the case: we also have to anticipate usage patterns.

To put that another way, you might not know at the time whether your function is going to be invoked from a high-throughput loop.

Is there a nice solution to this problem?

Re:Good Logging by wsargent on Saturday July 25, 2020 @01:30PM (#23950921)
> To put that another way, you might not know at the time whether your function is going to be invoked from a high-throughput loop.

> Is there a nice solution to this problem?

There's two ways to do it from within the logging framework. The first way is that you can suppress duplicate messages with a filter, like the DuplicateMessageFilter [1]. It wouldn't be that hard to add a filter that looked for the message template, and could suppress based on other properties on the LoggingEvent, such as the request ID or the overall similarity of the event.

The second thing you can do is add a budget for your logger, which will only allow it to log a certain period in a window. This is something you can do with budget evaluators [2].

Depending on the layer you put over SLF4J, you can also add conditional logging [3], which puts the logic at the application layer. Under this model, you'd have a counter or circuit breaker in the condition that would short-circuit the logging.

[1] http://logback.qos.ch/manual/filters.html#DuplicateMessageFi...

[2] https://tersesystems.github.io/terse-logback/guide/budget/

[3] https://tersesystems.github.io/blindsight/usage/conditional....

Re:Good Logging by eska on Friday July 24, 2020 @07:17PM (#23945466)
I think this is mostly a result of the OOP ethos to think of actions on single objects, where each function call created a log message. This is then called in a loop.

Meanwhile the general case is actually that actions are performed on a collection of things, although this is often not obvious due to OOP design. But when working with collections it becomes obvious that log messages should be aggregates over collections ("2 out of items failed, errors: ..."). This then creates magnitudes less of logging. It's just not mainstream now.

Re:Good Logging by maest on Friday July 24, 2020 @07:30AM (#23938193)
Sounds like you don't have enough information to make a decision when developing the function so thw logging behaviour should be configurable by the user.
Re:Good Logging by MaxBarraclough on Friday July 24, 2020 @07:50AM (#23938373)
Yes, I suppose a simple parameter would do the job. Probably not worth anything fancy.
Re:Good Logging by the_af on Friday July 24, 2020 @09:45AM (#23939391)
A parameter where? Obviously not in the code of the function itself; polluting your application code with flags such as log=true|false is obviously a no-go. Especially since logging is only one such "cross cutting concern", one of many, and you simply cannot go polluting your code with all of them because it leads to a mess.

Many Java logging systems allow you to enable/disable logging per package, but this is still not enough for a per function decision.

Logging is a hard problem.

Re:Good Logging by wsargent on Saturday July 25, 2020 @02:29PM (#23951367)
> Many Java logging systems allow you to enable/disable logging per package, but this is still not enough for a per function decision.

I have something for that. [1]

[1] https://tersesystems.github.io/blindsight/usage/conditional....

Re:Good Logging by BenoitP on Friday July 24, 2020 @11:07AM (#23940365)
Logging filtering is available per class in Logback [1], as well as having Markers which can target individual logging statements [2]

[1] https://stackoverflow.com/questions/46062711/show-logs-only-...

[2] https://stackoverflow.com/questions/30248169/how-to-deal-wit...

Re:Good Logging by Spivak on Friday July 24, 2020 @11:19AM (#23940518)
I mean there's no reason you couldn't have a thread-local override to the log level and restore it after the function call.

The fact that logging is at all intermixed with business logic I think is the main problem. I should be able to take a fully working logless program and apply different logging profiles to it. What gets logged should be a function of the person invoking the program.

Re:Good Logging by the_af on Friday July 24, 2020 @11:34AM (#23940724)
Definitely. I was trying to argue something about intermixing logging with app logic :) It indeed seems to me the decision about what to log should be the caller's, not the callee's, and so it shouldn't be hardcoded within the latter.
Re:Good Logging by Afton on Friday July 24, 2020 @11:39AM (#23940793)
I interpret your last line as "If an E2E test fails because of a product bug, the result should be a unit test".

I've been fighting this fight for a decade now, and I'm pretty sure I've never won it for a whole team. The best I got was that people would pre-populate bug fix code reviews with reasons why they couldn't sensibly unit test this particular issue. If you have had better success, please tell me your secrets!

Re:Good Logging by Chris2048 on Friday July 24, 2020 @07:42AM (#23938312)
> use a rate limiting logger with expiring delay

Hmm, any examples of this?

Re:Good Logging by wsargent on Saturday July 25, 2020 @01:34PM (#23950949)
>> use a rate limiting logger with expiring delay

> Hmm, any examples of this?

https://tersesystems.github.io/terse-logback/guide/budget/

Re:Good Logging by BenoitP on Friday July 24, 2020 @11:11AM (#23940416)
Re:Good Logging by usefulcat on Friday July 24, 2020 @09:18AM (#23939078)
You just have to remember the last time a particular log message was logged, and don't log it again until until some amount of time has passed.
Re:Good Logging by greggman3 on Friday July 24, 2020 @05:44AM (#23937553)
You forgot also making sure you aren't logging things you shouldn't log. Some are obvious maybe like passwords. Others might be less obvious like the title of every window on the user's desktop which Facebook Oculus logs. Those titles end up including every page of every website you've visited, the title of every document you've edited, and every video you've watched.
Re:Good Logging by wsargent on Saturday July 25, 2020 @01:37PM (#23950971)
Ideally the data should be filtered before it gets to logging in the first place, but if not, you can add another layer of defense by employing censors:

https://tersesystems.github.io/terse-logback/guide/censor/

Or you can use logstash-logback-encoder, which has masking:

https://github.com/logstash/logstash-logback-encoder#masking

Re:Good Logging by wsargent on Sunday July 26, 2020 @01:20PM (#23958527)
There's also Witchcraft Logging, which has the concept of "safe" parameters and is available in Go, Rust, and Clojure:

https://docs.rs/witchcraft-log/0.3.0/witchcraft_log/

Re:Good Logging by NetOpWibby on Friday July 24, 2020 @09:50PM (#23946248)
Is this a bug or a feature?
Re:Good Logging by crazy_hombre on Friday July 24, 2020 @01:15PM (#23941998)
> The viewer journalctl is probably the worst log viewing app that exists

Completely disagree there. I've had quite a pleasant experience with journalctl. It has sane arguments and you can easily grep any relevant log messages from the journal. You can add custom fields to your log messages. You can even get the logs in JSON format to parse log message from any scripts. And you can even use the cursor feature of journalctl to keep reading the logs at regular intervals without having to keep a constantly running process and tail the logs.

I can understand people not liking systemd-journald on account of the binary logs, but I definitely don't understand calling journalctl the worst log viewing app.

Re:Good Logging by masklinn on Friday July 24, 2020 @08:20AM (#23938594)
To me that's why the original promise of dtrace was so nice, the idea of being able to dynamically turn logging on and off from the outside at locations never intended to log anything without even touching the source code sounded wonderful.

Sadly $dayjob runs servers on linux and isn't going to merge the dtrace patches in there, and python's systemtap probes are… limited.

Apparently bcc can hook into USDT probes though, I should look into that: https://ish-ar.io/python_ebpf_tracing/

Re:Good Logging by wsargent on Saturday July 25, 2020 @01:51PM (#23951096)
I know this doesn't help for Python, but... You can do that in Java with instrumentation. Things like ByteBuddy [1] or ReactorDebugAgent [2] will add debugging statements into your code without you having to touch anything.

If you want more advanced, then you can look at libstapsdt [3] which enables JVM applications to create USDT probes.

[1] https://tersesystems.github.io/terse-logback/guide/instrumen...

[2] https://github.com/reactor/reactor-tools#reactor-debug-agent...

[3] https://github.com/goldshtn/libstapsdt-jni

Re:Good Logging by pjmlp on Friday July 24, 2020 @08:25AM (#23938630)
Depends on which flavour of Linux one goes by.

https://docs.oracle.com/en/operating-systems/oracle-linux/dt...

Regarding BPF, the book is a must have for anyone doing serious stuff with it.

https://www.informit.com/store/bpf-performance-tools-9780136...

Re:Good Logging by masklinn on Friday July 24, 2020 @08:26AM (#23938643)
I would strongly doubt the sanity of anyone getting their linux distro from oracle.
Re:Good Logging by pjmlp on Friday July 24, 2020 @08:31AM (#23938675)
Luckily most Fortune 500 management boards think otherwise, so I get to enjoy nice tools.
Re:Good Logging by kyuudou on Friday July 24, 2020 @05:22PM (#23944605)
In cases like HPC and other performance-centric workloads, mainline kernel just doesn't have the hardware support needed for more exotic stuff like Infiniband and Mellanox cards, high-end SSD etc. Oracle got tired of waiting for sufficient support to be included which is why IIRC OEL UEK was created.

If such hardware isn't being used, then yes, sanity should be questioned for supporting Uncle Larry's Linux.

Re:Good Logging by jimbob45 on Friday July 24, 2020 @04:53AM (#23937253)
I agree. Our architecture is partially built around logging every method called. Although it’s useful information, it still makes our product more difficult to program for.

Performance isn’t much of a problem though. Have different logging levels and only turn on a high level when you’re trying to nail down a particular STR.

Re:Good Logging by wsargent on Saturday July 25, 2020 @02:31PM (#23951377)
You can also hook up your logging to a feature flag system, so it's only activated for a single user. [1]

[1] https://tersesystems.com/blog/2019/07/22/targeted-diagnostic...

Re:Good Logging by wsargent on Saturday July 25, 2020 @02:27PM (#23951357)
> The second problem is to figure out if logging is affecting performance.

Spoiler -- it totally can. Kirk Pepperdine talks about logging as a memory allocation bottleneck [1]

So I gave my usual logging rant at a workshop that I gave. I was in the Netherlands about a year or so ago. And that night, they went and stripped all the logging out of their transactional monitoring framework that they’re using, which wasn’t getting them the performance they wanted, which is why I was there giving the workshop in the first place. And when they stripped out all their logging, the throughput jumped by a factor of four. And they’re going like, “Oh, so now our performance problem not only went away, we’ve been tuning this thing so long that actually, when they got rid of the real problem, it went much faster than they needed, like twice as fast as what they needed to go.” – The Trouble with Memory

Unfortunately it can be kind of hard to track the memory allocation rate over time, and it's typically not the sort of thing you're focused on. I put together an audio queue that will play the memory allocation rate as a sine wave [2] so I can tell when it's getting to be a problem.

[1] https://www.infoq.com/presentations/jvm-60-memory/

[2] https://tersesystems.com/blog/2020/07/19/listening-to-jvm-me...

Re:Good Logging by ed_elliott_asc on Friday July 24, 2020 @10:51AM (#23940175)
It is but if you log decisions you make and why and stupid things like variables at various stages then you at least have half a chance.