Manually clear log4j log file? - java

I've been able to find solutions to make the log file clear on each boot, but I don't want this to happen.
I only want to clear my log4j log file when we explicitly choose to, i.e. upon clicking a "clear log" button that calls an API to clear the log.
Is there a built-in log4j function to clear the log / how can I manually clear the log using Java code?

Related

Logback stops logging after clearing log file

I usually clear log files when I'm in developement mode and I need to have a fresh start to focus only on things I have to test.
If I clear a log file in linux (have not tested Windows), logback stops to write to that file
Maybe it's something about open handlers and file descriptors in linux.
How can I recover the situation without restarting the application?
Is it possibile to have an appender that can automatically recover this situation?
While your application is running (and Logback within your application has an open handle to the log file) ...
You won't be able to delete the file on Windows
You will be able to delete the file on Linux but as far as Logback is concerned the file still exists until Logback closes that open file handle. So, Logback won't know that the the file has been deleted but since the file has been deleted Logback cannot actually write anything to disk and this situation remains until Logback is re-initialised (and your FileAppender recreates the file). Typically, this will be done on application startup.
There's an open issue against Logback requesting a change in Logback's behaviour in this situation.
If you goal here is to have log output which focusses on recent-activity-only then you could define a rolling file appender with a minimal size and no history just to retain the (for example) last 1MB of data, this might help offer some focus on recent events only.
Alternatively, you'll have to:
Vote for this issue
Use grep/awk to find the relevant aspects of your log file (you can easily grep on timestamps) even if they are in a log file which contains the last few hours of log events
Ease the burden of restarting your application in this scenario by writing a simple script which does something like: (1) stop application; (2) delete log file; (3) start application

What to log and what not to log using log4j?

I'm newbie to logging event for java program using log4j. I would like to know what to log and what not to log inside my log file because writing to log file for everything we have done in program affects the performance. Also, sometimes we write the sensitive information to the log file, for example, database name. Thus I would like to have insight view about what we should not write and what we should write into our log file.
Avoid PCI and PII data in case your logs ever got into the wrong hands. Basically if you wouldn't want a hacker getting a piece of information, then don't log it.

How to display any type of error in java to the user while still logging it?

I am trying to find a centralized way to display errors to the user while logging them. Want to display the whole error message as it is written to the log without any customization. We are using log4j to log the errors and want to do minimal changes to the code.
Make a custom Appender as can be seen here. Add the Appender to your Logger and get notified about logging events and do with them whatever you want to.

Manage error log in Java

I have an issues with the file Error.log which is generate by Java.
It's too big (Currently >10Go) I can't open it with Notepad++/SublimeText etc.. and as it's on a dedicated computer, transfering it with Teamviewers make Teamviewer crash.
I would like to know if there is a way to configure how the error.log file is generated.
I want to have one file each days and only keep the last 7 days.
Can I configure Java to do that ? Or do I need to redirect System.err to a file ?
Thanks.
There are some java libraries you can use to manage log files the most popular log4j. So if you can edit the source code, this library can help achieve what you want. Besides that there are some tools that can handle large log files and give you search functionnality, edit reports and so on. try look for splunk, elasticsearch, kibana ..
If you have source code available just change log4j configuration. If not then try following
create a job which checks consistently to the log file and rename this when size exceeds some configurable value.

There is an interface/GUI appender for log4j?

I want to show my logs in the GUI in a textfield, list, etc. (something like ImgBurn), but I not found a ready appender that do this. There are an appender to do this?
Currently I doing log to the console without any problems.
Take a look at Apache Chainsaw: this can use the Receiver concept from the recent Log4j builds to display events: http://logging.apache.org/chainsaw/index.html.
If you are looking to do real-time text field display you may need to write your own Receiver class.
There are some good tools available for Free (paid as well) that can parse your log files and present them in better formats with lots of filtering and reporting options as well.
Here is a good list of such tools
Are you looking for a real time log viewer?
I can recommend you OtrosLogViewr. It can tail logs from running applications even on remote servers (using SFTP, FTP, Samba)
Check out the latest developer snapshot of Chainsaw - tons of new features, and when configured to process log files via VFSLogFilePatternReceiver, can tail log files, even between app restarts.
There are a ton of new features in this version compared with the currently-released one - some of the more helpful include search results that are highlighted and aggregated in a table, and the ability to annotate events (click in the 'marker' field)
http://people.apache.org/~sdeboy
Try Glasswall, which appends the log contents on the page if you are running a servlet.
http://code.google.com/p/glasswall/

Categories

Resources