Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging best practices and thoughts

Tags:

I'm about to do some refactoring off my app and I came to think about this simple yet complex topic, logging, how can it be so hard to do clean, effective and informative logging...

When you read documentation on logging you often see this snippet

if (BuildConfig.DEBUG) {     Log.d(TAG + "message"); } 

and it makes me wonder what the purpose is with it? According to the documentation, Android Developer - Log, the debug log messages are compiled in but stripped at runtime, so you wouldn't need to have the log call within that if statement. Or am I missunderstanding anything?

Then I'm also kind of wondering what the real benefit is with using any other Log.x() calls except debug as the log entries will not be seen by the user or logged into some errorfile, so they will be compiled in and executed in the production environment for no purpose at all? This is maybe a use case for the if statement before?

I mentioned earlier that the log entry isn't logged into a file. Why isn't this a built in feature in Android? Is it because of performance issues, unnecessary permission usage or something else? I have implemented this functionality in my own logging class, but now I wonder if it's bad practice? But it's also nice to have logs with important log entries?

So to wrap it up, to implement clean, effective and informative logging, both during development and in production. What's the best practices?

like image 343
Robert Avatar asked Sep 29 '14 21:09

Robert


People also ask

What is the importance of logging?

Provides necessary materials – Logging is a main source of timber which is used for a number of human needs such as providing construction materials, flooring wood, furniture, fuel for industries and homes, sports goods and other kinds of commodities.

What makes a good log message?

Instead, think of your log messages as events. They should describe one thing, with enough context to make it possible to see what's going on, or know where to go look for more information.

Why proper logging and monitoring is important?

Logging and monitoring are both valuable components to maintaining optimal application performance. Using a combination of logging tools and real-time monitoring systems helps improve observability and reduces the time spent sifting through log files to determine the root cause of performance problems.


1 Answers

Logs only needed to debug applications during development, to ensure that the feature works as expected and produces desired results. The best practice is, I believe, to do logging in any way which is most convenient to you and allows to find and resolve issues as quickly and efficiently as possible

I mentioned earlier that the log entry isn't logged into a file. Why isn't this a built in feature in Android?

Who (except a programmer on the development stage) would want an application to waste limited storage space on one's device with useless data? Users don't see, don't read, don't use logs. They don't need this garbage. Application in production must not produce any logs and, of course, must not save them to files.

The only logging which should be implemented in released application is unhandled exceptions logging. Moreover, it's application's responsibility to handle these logs if it suggests sending crash reports, and removing them after the report has been sent.

Another reason logs should not be created by released apps is that they may contain sensitive data and output which requires user authorization, thus introducing security flaws.

I believe the best practice is to remove all logs as soon as the module or feature is fully implemented and thoroughly tested, before deployment to production. Introducing if (BuildConfig.DEBUG) condition helps to ensure this has been achieved.

like image 144
Alexander Zhak Avatar answered Nov 15 '22 10:11

Alexander Zhak