Log4sh:Examples 1.4
From FeProductWiki
See also examples for 1.3
Examples and Workarounds for log4sh 1.4
|
Logging using two separate appenders
Contributed by Laurent Georges.
The idea is to declare an appender in the log4sh.properties (called here HISTORY).
# Set root logger level to INFO and its only appender to A1 and HISTORY log4sh.rootLogger = INFO, A1, HISTORY
At the beginning of your script, close the appender HISTORY, this way, no logs will be written in it.
# we close the HISTORY appender to avoid logging in it appender_close HISTORY
Develop a little function which logs specifically in your appender with a given level:
history_info()
{
# we reactivate the specific HISTORY appender
appender_setLevel HISTORY INFO
log_to_appender HISTORY INFO $1
# we close the specific appender HISTORY
appender_close HISTORY
}
The function above depends on extending log4sh, either by editing log4sh directly, or adding the function to the script where you need it.
#/**
# <s:function group="Logger" modifier="public">
# <entry align="right">
# <emphasis>void</emphasis>
# </entry>
# <entry>
# <funcsynopsis>
# <funcprototype>
# <funcdef><function>log_to_appender</function></funcdef>
# <paramdef>string <parameter>appender name</parameter></paramdef>
# <paramdef>string <parameter>level</parameter></paramdef>
# <paramdef>string[] <parameter>message(s)</parameter></paramdef>
# </funcprototype>
# </funcsynopsis>
# <para>This function logs a message to a specific appender </para>
# <funcsynopsis>
# <funcsynopsisinfo>log A1 DEBUG 'This is a test message'</funcsynopsisinfo>
# </funcsynopsis>
# </entry>
# </s:function>
#*/
log_to_appender()
{
_l_appenderName=$1
shift
_l_level=$1
shift
# if no message was passed, read it from STDIN
[ $# -ne 0 ] && _l_msg="$@" || _l_msg=`cat`
_l_levelInt=`logger_level_toInt ${_l_level}`
# update seconds elapsed
_log4sh_updateSeconds
_l_oldIFS=${IFS} IFS=${__LOG4SH_IFS_DEFAULT}
${__LOG4SH_TRACE} "_l_appenderName='${_l_appenderName}'"
# determine appender level
_l_appenderLevel=`appender_getLevel ${_l_appenderName}`
if [ "${_l_appenderLevel}" = "${__LOG4SH_NULL}" ]; then
# continue if requested is level less than general level
[ ! ${__log4shLevel} -le ${_l_levelInt} ] && DoNothing=1
else
_l_appenderLevelInt=`logger_level_toInt ${_l_appenderLevel}`
# continue if requested level is less than specific appender level
${__LOG4SH_TRACE} "_l_levelInt='${_l_levelInt}' _l_appenderLevelInt='${_l_appenderLevelInt}'"
[ ! ${_l_appenderLevelInt} -le ${_l_levelInt} ] && DoNothing=1
fi
# execute dynamic appender function
if [[ $DoNothing != 1 ]]; then
${__LOG4SH_APPENDER_FUNC_PREFIX}${_l_appenderName}_append ${_l_level} "${_l_msg}"
fi
IFS=${_l_oldIFS}
unset _l_msg _l_oldIFS _l_level _l_levelInt
unset _l_appenderLevel _l_appenderLevelInt _l_appenderName DoNothing
}
This workaround has several drawbacks:
- You have to develop one function for each logging level you want to use (as we can not specify 2 different loggers in the properties file)
- It forces the log level (I did not find any other way as the CLOSE statement is in fact managed as a logging level)
