Configuring Rolling File Appender - jboss

My jboss application server log file named jboss-log4j.xml has the following configuration for the rolling file appender
<appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/server.log"/>
<param name="Append" value="false"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
What does this exactly do ?
How should I set it to do either of the two things ?
1. Delete logs older than 5 days or
2. Delete logs when they exceed 800MB, so that once they are deleted the new logs can take effect

Most of the parameters are explained in the manual I linked to your earlier question here. Even if that shows the parameters in a property file, the name and semantics of the parameters is the same.
AFAIK you can't easily make a log file roll over every 5 days. The closest to this is weekly rollover. For this you should use a DailyRollingFileAppender as I showed in my previous answer, and set its date pattern to
<param name="DatePattern" value="'.'yyyy-ww"/>
See the usage options of DailyRollingFileAppender here.
Setting the size limit of the RollingFileAppender shown in your post to 800MB instead of 500KB is a task I am sure you can resolve yourself ;-)
So you can easily have a log file rolling over either at the start of every week or upon reaching a specified size limit. However, I have a vague recollection that the two don't go together. You either have a DailyRollingFileAppender or a RollingFileAppender - Log4J doesn't provide an appender with both functionality at the same time. Neither to roll over your log file every 5 days - your only option to achieve this is to implement your own custom appender.

You ask how to delete logs older than 5 days. Typically, we will setup a cron job that uses find to delete old log files and zip files that are not quite as old. For example:
find /apps/atg/$JBOSS_VERSION/jboss-as/server/${inst_name}/log \
-name "*.log.2*" -type f -mtime +14 -exec rm {} \;
find /apps/atg/$JBOSS_VERSION/jboss-as/server/${inst_name}/log \
-name "*.log.2*" ! -name "*.gz" -type f -mtime +3 -exec gzip {} \;

Related

log4j writes blank lines in console

I have the following log4j configuration:
<appender name="MYCONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="INFO"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}|%-5p|MYAPP|%t|%C{1}.%M(%L)|%m%n"/>
</layout>
</appender>
<appender name="MYFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${jboss.server.log.dir}/myapp.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}|%-5p|MYAPP|%t|%C{1}.%M(%L)|%m%n"/>
</layout>
</appender>
In the Eclipse console, every log line produced by this configuration is followed by a completely blank line:
12:46:27,289 INFO [stdout] 2017-01-25 12:46:27,289|INFO|MYAPP|something
12:46:27,289 INFO [stdout] 2017-01-25 12:46:27,289|INFO|MYAPP|something else
This happens only with logs generated by MYCONSOLE, all other logs (such as the internal JBoss's ones) don't have blank lines.
Also, in the MYFILE log file, the logs are the same but the blank lines are not there.
What could be the reason?
I was able to get rid of the extra lines by using \n instead of %n.
The reason of the strange behaviour of %n is still not clear to me.
Getting same to me when I use consoleAppader for audit logger
log4j.appender.audit=org.apache.log4j.ConsoleAppender
log4j.appender.audit.layout=org.apache.log4j.PatternLayout
log4j.appender.audit.Target=System.out
log4j.appender.audit.ConversionPattern=%d{ISO8601}|%t|%m%n
The logs dumping on console are empty/blank lines.
If I remove I can get rid of empty lines, I am missing actual data too.
Would appreciate any pointers

How to return a value from a phing target?

I would like to have a foreach task like this, which iterates over all the files/directories in a directory "A" -
<foreach param="dirname" absparam="absname" target="subtask">
<fileset dir="${dir.destination}/${dir.subdir}/">
<type type="file" />
</fileset>
</foreach>
The target "subtask" should check if the counterpart of the file/folder exists in another directory "B" (I am comparing directory A and B basically), and return either of the following if it does not -
a flag.
name of the file.
Following is some code for reference -
<target name="subtask">
<if>
<filesmatch file1="${file1}" file2="${file2}"/>
<then>
Return false. But how?
</then>
<else>
Return true of name of the file. How?
</else>
</if>
</target>
Note - It is okay if this can be done without calling a target. I am not sure if the logic can be fit inside the foreachtask itself. Could not find any such thing in the phing documentation.
Basically, I should be having the list of file names which are not present in the directory B, by the end of the loop.
You may also read this question of mine, if you can give some pointers to solve the issue in some other way.
Update
Rephrasing this question, since I feel that the problem description is not clear. The phing documentation says, a target has no return value -
Targets are collections of project components (but not other targets)
that are assigned a unique name within their project. A target
generally performs a specific task -- or calls other targets that
perform specific tasks -- and therefore a target is a bit like a
function (but a target has no return value).
I don't understand why is it designed so. With this bounty, I would like to know if there is some workaround for me other than having to define my own custom tasks in PHP, and then set properties -
$this->getProject()->setNewProperty('modifiedElements', implode("\n\n",$modifiedElementsArray));
which can be accessed in the build file
I have a target which checks whether my production code base has any differences from the expected git revision -
<target name="compare_prod_with_expected_revision">
<input propertyname="box.git_version">
Enter git version of the production codebase:
</input>
<exec command="git reset --hard ${box.git_version}" dir="${dir.scratchpad}" />
<!-- Scratchpad brought to revision ${box.git_version} -->
<echo>Verifying whether production is at revision ${box.git_version}..</echo>
<exec command="diff -arq --exclude='.git' ${dir.scratchpad}/${dir.subdir} ${dir.destination}/${dir.subdir}" outputProperty="diffList"/><!-- #TODO ignore.swp files in this step. Diff says .swp files present in production code. But doing ls -a there does not show the same. -->
<php function="strlen" returnProperty="productionDeviationFromExpectedBranch"><!-- #TODO - find how to not show this step during build process. Put it in a target and set hidden="true" -->
<param value="${diffList}"/>
</php>
<if>
<equals arg1="${productionDeviationFromExpectedBranch}" arg2="0" />
<then>
<echo>Verified production is at revision ${box.git_version}</echo>
</then>
<else>
<echo>Differences - </echo>
<echo>${diffList}</echo>
</else>
</if>
</target>
Now, I want to phingcall this target and would like to access some property set by it.
I think I understood your purposes, and at the same time I feel like you chosen not the optimal tool of doing this.
As you mentioned in your question, official documentation on phing is clear about tasks (targets):
Targets are collections of project components (but not other targets) that are assigned a unique name within their project. A target generally performs a specific task -- or calls other targets that perform specific tasks -- and therefore a target is a bit like a function (but a target has no return value).
Targets should be components of your application, which execute specific task, atomic task. It could be initialization task, configuration fetching, compilation step, assets preparation and dumping, deployment task, clean-up task, etc. There's no "output", returned by target in the standard sense, but the result of target execution is the success of execution itself: success or failure.
One should not try to put way too much of logic into such project targets, as it is not intended to do complicated calculations, do heavy logical decisions, etc. I mean, Phing can do it, such things are possible, but this setup would be bulky, unreadable, and hard to scale/re-factor.
With Phing you can easily define conditional execution and branching of logical flow, you may define the sequence of execution of tasks (dependencies) - this is what makes it laconic and elegant. Keep targets as simple as possible, split the project into small, finished logical tasks.
Based on the projects I've been working with, the biggest targets, probably, were initialization stage and configs fetching. Here's some example, to understand what it might contain, I took it from real project:
<target name="init_configuration">
<echo msg="Define initial configuration for the deployment..." />
<if>
<not>
<isset property="host" />
</not>
<then>
<property name="host" value="dev" override="true" />
<echo message="The value of hostname has been set to ${host}" />
</then>
<else>
<echo message="The value of hostname is ${host}" />
</else>
</if>
<if>
<not>
<isset property="version" />
</not>
<then>
<property name="version" value="1.0.0" override="true" />
<echo message="The value of version has been set to ${version}" />
</then>
<else>
<echo message="The value of version is ${version}" />
</else>
</if>
<property name="host_credital_file" value="config/hosts/${host}.properties" />
<property file="${host_credital_file}" />
<available file="${host_credital_file}" property="hostfilefound" value="true"/>
<fail unless="hostfilefound" message="Missing Hostfile configuration file (${host_credital_file})!" />
<echo msg="Configuration is done" />
</target>
Other targets were extremely simplistic, they are normally – 1-5 lines long, and do only small purpose, small task. This would be, probably, the best recommendation when working with Phing.
The logic which you are trying to put on shoulders of Phing is possible, but would be extremely bulky.
Consider the point: how much quicker, easier, and more readable the same thing could be done with simple bash script in your example. Or even to write small CLI utility in PHP, which will do the job elegantly and quick. After that in Phing you'll leave parametric target which will execute this "revision diff script" from CLI.
Phing is a great tool for what it is designed for, but it can't be an optimal choice for every purpose. Just do not put way to much responsibility and logic into it.
As a workaround, for more complicated things it's better to combine Phing with with something specialized: bash scripting, PHP CLI, nodeJS (+ Grunt, Gulp, etc)... and just to add calls of a Phing targets later.
This is the way I managed to have targets which behave like functions:
<target name="-console-cmd-return-property" hidden="true">
<exec command="${command}" checkreturn="${checkreturn}" logoutput="${logoutput}" outputProperty="${outputProperty}"/>
</target>
It gets invoked like this:
<phingcall target="--console-return-property">
<property name="command" value="ps auxwww"/>
<property name="checkreturn" value="true"/>
<property name="logoutput" value="false"/>
<property name="outputProperty" value="ps_output"/>
</phingcall>
Of course it works because it relies on existing exec, and it is not generic...
The target "subtask" should check if the counterpart of the file/folder exists in another directory "B" (I am comparing directory A and B basically), and return either of the following if it does not -
a flag.
name of the file.
You could compare two directories without using a foreach task like this:
<project name="Phing Build Test" default="print-missing" basedir=".">
<resolvepath propertyName="dir.a" path="path/to/dir/a"/>
<resolvepath propertyName="dir.b" path="path/to/dir/b"/>
<target name="print-missing">
<apply executable="echo" failonerror="false" returnProperty="files.found" outputProperty="missing">
<srcfile/>
<fileset id="srcfiles" dir="${dir.a}" includes="*">
<present present="srconly" targetdir="${dir.b}"/>
</fileset>
</apply>
<if>
<equals arg1="${files.found}" arg2="0"/>
<then>
<echo msg="${missing}"/>
</then>
</if>
</target>
</project>

testdriven.net log4net console output

How do I get log4net console output to show up when using TestDriven.net to run nunit tests? I'm using the ColoredConsoleAppender, and there is no log output.
I've found the answer. You can use the regular xml config file approach, but you need to configure a TraceAppender:
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %logger - %message%newline" />
</layout>
</appender>

First run notepad with my.cfg and only then start the service

I install along with my application:
1) a service that starts and stops my application as needed
2) a conf file that contains actually the user data and that will be shown to the user to modify as needed (I give the user the chance to change it by running notepad.exe with my conf file during installing)
The problem is that in my code the service I install starts before the user had the chance to modify the conf file. What I would like is:
1) first the user gets the chance to change the conf file (run notepad.exe with the conf file)
2) only afterward start the service
<Component Id="MyService.exe" Guid="GUID">
<File Id="MyService.exe" Source="MyService.exe" Name="MyService.exe" KeyPath="yes" Checksum="yes" />
<ServiceInstall Id='ServiceInstall' DisplayName='MyService' Name='MyService' ErrorControl='normal' Start='auto' Type='ownProcess' Vital='yes'/>
<ServiceControl Id='ServiceControl' Name='MyService' Start='install' Stop='both' Remove='uninstall'/>
</Component>
<Component Id="my.conf" Guid="" NeverOverwrite="yes">
<File Id="my.cfg" Source="my.cfg_template" Name="my.cfg" KeyPath="yes" />
</Component>
[...]
<Property Id="NOTEPAD">Notepad.exe</Property>
<CustomAction Id="LaunchConfFile" Property="NOTEPAD" ExeCommand="[INSTALLDIR]my.cfg" Return="ignore" Impersonate="no" Execute="deferred"/>
<!--Run only on installs-->
<InstallExecuteSequence>
<Custom Action='LaunchConfFile' Before='InstallFinalize'>(NOT Installed) AND (NOT UPGRADINGPRODUCTCODE)</Custom>
</InstallExecuteSequence>
What am I doing wrong in the above code and how could I change it in order to achieve what I need? (first run notepad with my conf file and then start the service).
I would extend the MSI UI to ask for the parts the user needs to modify and then update the text file using XmlFile and XmlConfig elements. Then Windows installer can come by and start the service.

I already have the log file. How can I make a graph of it?

I already have log file. Now what i need is to make the graph of that log file.
Is there any command line tool or google API to make graph which will show every transaction time.
Thank You
You can upload your file to Google Docs as a spreadsheet and then do charting & graphs out of it.
I use something like this:
java -jar perf4j-0.9.13.jar /path/to/perf4jLog.log -t 86400000 -g out.html
Note here where i use -t 86400000 which means collate the data via days. Use whatever is applicable for you.
My log4j.xml looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="perf4jLog" class="org.apache.log4j.DailyRollingFileAppender">
<param value="perf4jLog.log" name="File"/>
<param value="'.'yyyy-MM-dd" name="DatePattern"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="org.perf4j.TimingLogger">
<level value="INFO"/>
<appender-ref ref="perf4jLog"/>
</logger>
<root>
<priority value ="INFO"/>
</root>
</log4j:configuration>
Now you can open up out.html and the images should load the chart from google.