Log4J - Auto refresh log files, is it possible? - java

I've started to make tests, and all the output is sent to a ".log" file. In one test, I have to stop manually the application in a certain line of log.
The question is: Can a .log file work like a Console, displaying info in real time?

You can monitor a log file with tail -f https://en.wikipedia.org/wiki/Tail_%28Unix%29
Within your java code you can monitor its output see java runtime.getruntime() getting output from executing a command line program
or create your own appender How to create a own Appender in log4j?

Related

How to truncate Tomcat Catalina.out log to 3 months back

My company wants us to save logs from the past 3 months. Catalina.out is getting too large on the linux server (Red hat). How can I remove everything in the log except for the last 3 months in the ONE catalina.out file.
I had a requirement slightly similar to you case. This is how I tackled the requirement.
Add following Linux commands into Cron Scheduler
grep the required date stamp i.e. 2022-05-07 and write to new file
grep "2022-05-07" > 2022_05_07.log // write lines which matches the data stamp
wc -l 2022_05_07.log // outputs how many lines captured
Clear the log of catalina.out file without stopping tomcat with the command below.
sudo cat /dev/null > /opt/tomcat/apache-tomcat-9.0.37/logs/catalina.out
Make note it is not recommended to delete the catalina.out while tomcat is running.
It will keep on logging to catalina.out which is removed already (reference of the file is hold by the tomcat) hence the space will not be released. So you will need to restart the tomcat sever to release the space.

Tomcat catalina.out is 40GB

I wonder why my spring project with tomcat server got catalina.out file with size 40GB. Any solutions, please.catalina.out reach 40 GB
catalina.out reaches such a large size because:
1- there might be many logging messages sent to console handler, and
2- also there is not any rotation of catalina.out (and no policy to remove older catalina.out).
First, as there might be some duplication and the messages in catalina.out , which could also be stored in *log messages too, I'd check if the contents of the log files (catalina.[DATE].log) are the same as those of catalina.out, if so then you can edit file conf/logging.properties and remove console handler
I'd also check the level of the log messages and set a higher level if possible. Look for this line in conf/logging.properties
java.util.logging.ConsoleHandler.level = ....
Possible levels, in increasing level of frequency are SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST or ALL. I'd try replace ALL, FINEST, FINER, FINE by CONFIG or even INFO. For instance, by setting it to INFO, all SEVERE, WARNING and INFO messages will be logged but not any with a level to the right of that list.
Also another option is set a limit to console handler by adding this line to conf/logging.properties
java.util.logging.ConsoleHandler.limit = 1024000
and rotate catalina.out configuring an automatic task to remove older ones.
if you are linux user to handle this from system is pretty easy you can configure logrotation with logrotate this is very easy
Step : 1 (Create Logrotate file)
root#c2dapp01-usea1e# vim /etc/logrotate.d/tomcat
Step : Add rotation instruction for linux log rotator
/opt/tomcat/latest/logs/catalina.out {
copytruncate
daily
rotate 7
compress
missingok
size 100M
}
Step : 3 Add cron job to run daily in crond.daily or create custom cron (This file is by default there if not then only create)
root#c2dapp01-usea1e:# vim /etc/cron.daily/logrotate
# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
[ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
This script can be run manually.
/usr/sbin/logrotate /etc/logrotate.conf
Most probably the huge size of logs is due to the DEBUG mode setting of log4j. Change this setting to WARN and the size of logging will be reduced.
This is quite common to see catalina.out file expanding.
this could be used to clear the log of catalina.out file without stopping tomcat with the command below.
sudo cat /dev/null > /opt/tomcat/apache-tomcat-9.0.37/logs/catalina.out
To keep catalina.out smaller in size, either one of the following ways can be used:
You could use the above Linux command and add it to a CronScheduler
to run daily/ weekly/ monthly or yearly as preferred.
Use logrotate tool in Linux. It is a log managing command-line tool
in Linux. This can rotate the log files under different conditions.
Particularly, we can rotate log files on a fixed duration or if the
file has grown to a certain size. You can click here for more
info on this.

how to command line result save into textfile

i have following code i want command line result save in my textfile also how do this. please help
#echo off
set JAVA_HOME=C:\jdk1.5.0_05
set CLI_HOME=c:\projects\utds\applications\cli
set CLI_LIB=%CLI_HOME%\lib
set CLASSPATH=%CLI_LIB%\commons-logging.jar;%CLI_LIB%\commons-logging-api.jar
set CLASSPATH=%CLASSPATH%;%CLI_LIB%\spring.jar;%CLI_LIB%\spring-core.jar;%CLI_LIB%\spring-support.jar;%CLI_LIB%\spring-remoting.jar
set CLASSPATH=%CLASSPATH%;%CLI_LIB%\utds-infra.jar;%CLI_HOME%\src\conf\spring;%CLI_HOME%\src\conf
set CLASSPATH=%CLASSPATH%;%CLI_LIB%\aopalliance.jar
set CLASSPATH=%CLASSPATH%;%CLI_HOME%\dist\cli.jar;%JAVA_HOME%\jre\lib\ext\comm.jar
set path=%JAVA_HOME%\bin;%path%
java -Dport=COM3 -DbaudRate=19200 -Dparser=panasonicCliParser -DappContext=applicationContext-service.xml com.utds.cli.service.comm.CallerIdListener
I would pipe the output of the batch command into a text file by running the following command in the command prompt:
myBatchFile.bat > output.log
Okay it looks like you're trying to put the output of the program into a text file. If that is the case, in your code just add:
java > log.txt
In my opinion, you should better use your logging library. I can see from the script here above that your applications uses Apache's commons-logging and the output shows it is clearly used.
This library is a wrapper indeed. It can use Log4J or JDK's logging library under the hood.
Of course, this requires much more learning and struggling with configuration files but the advantage for you is that you could (following the implementation you chose):
Filter logs following their gravity (debug < info < warning < error...) and/or the classes emitting them. Some libraries are quite verbose .
Create rolling log files : once the the log file reaches a certain size, a new log file can be created and the old one is backup-ed. (It can be possible to limit the number of backups...).
Create a log file per day
Log into databases if you ever need it...
....
add only >mylog.txt Thanks All
java -Dport=COM3 -DbaudRate=19200 -Dparser=panasonicCliParser -DappContext=applicationContext-service.xml com.utds.cli.service.comm.CallerIdListener> mylogs.txt

Waiting for batch process to finish before continuing with the rest of the codes in .java file

I have this java file, where I need to invoke a batch file from,
for this, I use the following line
Process proc = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+ "D:\\Builds\\automation\\INSTALLER\\SOURCE\\A.bat");
This runs fine, the bat file is invoked in command prompt.
A.bat also has a few lines where 2 other bat files are called. You can consider them as B.bat & C.bat
After all these Batch files are run, there is a output.log file that is generated & has information if the process was "successful" or there were "errors". So based on the words in the log, I will need to either print "SUCCESS" or "ERROR".
Now, my problem is, I used
proc.waitFor();
so that the java file wait for the Process to finish before the remaining codes are run.
This just does not happen. Even before B.bat is run, "SUCCESS" gets printed.
Is there a way I could edit the batch file according to my needs or edit the java file to wait till the process completes?

Cannot get External Node Classifier in puppet working - bash and java

I have the following script where I call a java program that writes a YAML output to Strandard output stream, and that is echoed (Simple).
#!/bin/bash
echo `/usr/lib/jvm/jre/bin/java -jar /etc/puppet/enc/enc.jar $1`
I have the above script in file/etc/puppet/enc/javaEnc.sh When I execute this providing node name as argument I get the following output.
---
classes:
class1:
class2:
The problem is, on the agent node, I get the error message
err: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find node 'node-agent-1'; cannot compile
warning: Not using cache on failed catalog
err: Could not retrieve catalog; skipping run
I have found that the script does not execute (or rather my java program is not called, don't know why) - In my java program I write the output to a file in addition to doing a System.out.print.
I have a another script where I read the file (data.yaml) that contains the same data as I mentioned as output and writes it to output stream by following script.
#!/bin/bash
cat "/etc/puppet/enc/data.yaml"
When this script is mentioned against external_nodes, it works fine, the puppet agent configures itself. Can I please get an idea where I am getting it wrong.? The java program actually queries some external resources and classifies the classes and produces the output - it takes around 10 seconds to get this done. Could this be a problem ? I have seen ruby and python solutions - couldn't get them to work either. I would like it done with Java most preferably.
In my puppet.conf file I have the following.
[master]
node_terminus = exec
external_nodes = /etc/puppet/enc/javaEnc.sh

Categories

Resources