TWiki
>
WebXEO Web
>
XEOECCXEOCoreSystem
>
XEOPrimerLogginInXEOApplicationsWithLog4j
(revision 6) (raw view)
Edit
Attach
Tags:
tag this topic
create new tag
,
view all tags
---++++++ -- Main.JoaoCarreira - 15 Dec 2009 ---++++++ General Description In XEO a logging system was created, to create a level of abstraction from other loggers currently used. In this version the log4j was updated to version 1.2.15 wich is used internally by the XEO. <!--No xeo foi criado um sistema de logging de forma a criar um nível de abstração sobre os loggers actualmente utilizados. Nesta versão o log4j foi acualizado para a versão 1.2.15 que é utilizado internffamente pelo XEO.--> <!--Esta implementação pretende normalizar o logging no XEO assim como em aplicações desenvolvidas sobre a plataforma.--> This implementation pretends to normalize the XEO logging as also from the applications developed in this platform. <!--Se não for especificado qualquer configuração no boconfig.xml, o sistema utiliza a parameterização geral do log4j, ou seja o log4j.properties.--> If no configuration is specified in the boconfig.xml, the system will use log4j general parameterization, namely log4j.properties. ---++++++ Logger Architecture The logger system consits of two levels. The Logger and the Appender <!--O sistema de logger é constituido por dois níveis. O logger e ou Appender.--> The loggers are the elements that define wich packages schould be logged, as also the writting format in the log. For each logger there are appenders, wich can be the following: *console - This appender writes the log messages of the standart java output *file - This appender creates log files with a defined file size *email - This appender sends error by email to the configured recipients. This logger does not work in every leves, just superiors to WARNING. <!--Os loggers são os elementos que definem quais os packages que devem ser logados assim como o formato de escrita no log. Por cada logger existem appenders, que podem ser os seguintes: * console - Este appender escreve as mensagens de log na standard output do java * file - Este appender cria ficheiros de log com um limite pre-definido de tamanho * email - Este appender envia os erros por email para os destinatários configurados. Este logger não funciona em todos os níveis, só superiores a WARNING.--> There can be several loggers with different appenders, if you pretend that different packages are logged in different ways. <!--Podem existir varios loggers com diferentes appenders, caso se pretenda que determinados packages sejam logados de formas distinctas.--> ---++++++ How to use *Obtain a logger for a class* <verbatim>import netgest.bo.system.Logger; (...) private static final Logger logger = Logger.getLogger(MyClass.class);</verbatim> *Write on the log* <verbatim>logger.warn("Something went wrong. Message");</verbatim> ---++++++ Performance When using logging, you should pay some attention to perfiormance questions. For example, avoid messages to be created if the logger is not active for the current level. <!--Quando se está a fazer logging deve-se ter alguma atenção quanto às questões de performance. Por exemplo, evitar que mensagens sejam criadas se o logger não está activo para determinado nível.--> When doing logging, You should put the code in a if wich will check if the logger is active for the current level. <!--Quando se está a fazer loging deverá sempre colocar-se o código dentro de um if que interrogará o logger se este está activo para determinado nível.--> *Wrong* <verbatim>logger.finest("Entering class " + this.getClass().getName );</verbatim> *Right* <verbatim>if( logger.isFinestEnabled() ) logger.finest("Entering class %s", this.getClass().getName ); </verbatim> By the second way, we avoid the string to be concatenated with the cass name when the FINEST level is not active. <!--Na segunda forma, evitamos que a string seja concatenada com o nome da class quando o nível FINEST não está activo.--> ---++++++ XEO Logging configuration ( boconfig.xml ) Logging levels and respective using cases: | *#* | *Level* | *Log Text* | *Description* | | 1 | FINEST | FINST | This level should be used to behavior debug. This level should never be active in production | | 2 | FINER | FINER | This level should give relevant information to trace errors, how external API's are called, runtime configuration reading results.<!--nível deve dar informação relevante de trace de erros, como seja chamada a API's externas, resultados de leitura de configurações de runtime--> | | 3 | FINE | FINE | Extended log level, gives information about backgroud tasks execution, external API calls result like sending emails, web srvice calls, etc.<!--Nível de log alargado, ou seja dá informação sobre execução de tarefas de background, resultado de chamadas a API externas como envio de emails, chamadas a webservices, etc. -->| | 4 | CONFIG | CONF | Information about System configurations, Atention, do not log passwords | | 5 | WARNING | WARN | Information of critical warning, unusual errors, but of wich ones the user is notified if they occurred. | | 6 | SEVERE | SEVER | Errors that prevent a good system functioning, so as poorly written queries, viewers/objects with definition syntax error, etc.<!-- que impedem o bom funcionamento do sistema, tais como queries mal escritas, viewers/objectos com erros de sintaxe na definição, etc...--> | Configuration in boconfig.xml General configuration <literal>logger:</literal> | *Attribute* | *Possible Values* | *Description* | | active | true/false | If the logger is active or not | | for | String separated by commas | Packages for wich this configuration should be used <!--que esta configuração para os quais esta configuração deve ser usada-->| | level | FINEST / FINER / FINE / CONFIG / WARNING / SERVERE | Log level that will be done by this configuration <!--Nivel de log que deve ser feito por esta configuração -->| | pattern | Writting format for the log of type log4j | Log writting format | Appender Configuration <literal>console:</literal> | *Attribute* | *Possible Values* | *Description* | | active | true/false | Activate or disable this appender | Appender Configuration <literal>file:</literal> | *Attribute* | *Possible Values* | *Description* | | active | true/false | Activate or disable this appender | | <p>logFile</p> | <p>Name of the log file</p> | <p>xeoHome relative path or absolute localization and log file name</p> | | maxSize | nn / nnKB / nnMB | Maximum log file size. | | backupFiles | n | Number of files that should be kept in the history after log limit exeded| Appender Configuration (This Appender only works for logs superior to WARNING) <literal>email:</literal> | *Attribute* | *Possible Values* | *Description* | | active | true/false | Activate or disable this appender | | buffer | n | Number of earlier messages in the log that should be send in the email body | | smtpHost | hostname | SMTP Server to use for sending error logs | | from | email address | Sender email address | | to | email address | Receiver email address | | cc | email address | Email CC address | | bcc | email address | Email BCC address | | subject | String | Email subject text | ---++++++ Configuration Examples This logger is configured to do logging in all classes that are in the pt and netgest package hierarchy. The log level is FINEST so, the log will be long. Once the appender email is deactivated, the WARNING and SEVERE messages are not send by email. <!--Este logger está configurado para fazer logging em todas as classes que estejam na hierarquia do package pt e netgest. O nivel de log é o FINEST por isso, o log será extenso. Como o appender email está desactivado, as mensagens de WARNING e SEVERE não serão enviadas por email.--> <verbatim><logConfig> <logger active='true' for='pt,netgest' level='FINEST' pattern='%d %5p [%t] (%F:%L) - %m%n' > <console active='true' /> <file active='true' logFile='.\log\fileName.log' backupFiles='5' maxSize='50MB' /> <email active='false' buffer='50' smtpHost='mail.itds.pt' from='' to='jp[a]itds.pt' cc='' bcc='' subject='ola' /> </logger> </logConfig> </verbatim> Log with examples of different leves and appenders by package. This configuration makes the log in FINEST mode to the console, file and email of all classes that are in the package pt. For the classes in the netgest package, writes on the console only config messages or higher. <!--Log com exemplo de niveis diferentes e appenders por package. Esta configuração faz o log em mode FINEST para a consola, ficheiro e email de todas as classes que estejam no package pt. Para as classes que estejam no package netgest, escreve na consola apenas as mensagens de CONFIG ou superiores.--> <pre><verbatim><logConfig> <logger active='true' for='pt' level='FINEST' pattern='%d %5p [%t] (%F:%L) - %m%n' > <console active='true' /> <file active='true' logFile='.\log\fileName.log' backupFiles='5' maxSize='50MB' /> <email active='true' buffer='50' smtpHost='mail.itds.pt' from='' to='destemail' cc='' bcc='' subject='ola' /> </logger> <logger active='true' for='netgest' level='CONFIG' pattern='%d %5p [%t] (%F:%L) - %m%n' > <console active='true' /> </logger> </logConfig> </verbatim> </pre> ---++++++ Example of a class using logging Example of use in a deveoped class: <verbatim style="padding-left: 30px; ">package pt.logger; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import netgest.bo.system.Logger; import netgest.bo.system.boApplication; public class LoggerDemo { // Initialize the logger for the class private static final Logger logger = Logger.getLogger( LoggerDemo.class ); public static void main( String[] args ) { // Call some sample methods loggedMethod(); doSomeWork(); } public static void doSomeWork() { FileWriter fw; PrintWriter pw; File propsFile; // Log configuration info to the logger logger.config("xeoHome is [%s]", boApplication.getApplicationFromStaticContext("XEO") .getApplicationConfig().getNgtHome() ); propsFile = new File("c:\\jvmprops.txt"); if( propsFile.exists() ) { // Warning - the previous file was overwritten logger.warn("Overwritting file %s ", propsFile.getName() ); } else { if( logger.isFineEnabled() ) // Fine - Creating a file logger.fine("Creating file %s ", propsFile.getName() ); } try { fw = new FileWriter( propsFile ); pw = new PrintWriter( fw ); if( logger.isFinestEnabled() ) { // Finest - Filling a file logger.finest("Dumping properties to [%s]", propsFile.getAbsolutePath() ); } for( Object propName : System.getProperties().keySet() ) { pw.format( "%s : %s \n", propName, System.getProperty( propName.toString() ) ); } pw.close(); if( logger.isFinestEnabled() ) { // Finest - Closing the file logger.finest("Closing file [%s]", propsFile.getAbsolutePath() ); } fw.close(); } catch (IOException e) { // Severe - Something goes wrong writing the file logger.severe( "Error writing to file [%s]" , e, propsFile.getAbsolutePath() ); } } public static void loggedMethod() { // Finest - tracing method call's logger.finest( "Entering in method: %s", "loggedMethod" ); // Do Work // (...) logger.finest( "Exiting in method: %s", "loggedMethod" ); } } </verbatim>
Attachments
Attachments
Topic attachments
I
Attachment
Action
Size
Date
Who
Comment
java
LoggerDemo.java
manage
2.1 K
2009-12-21 - 20:49
JoaoCarreira
Código fonte da class de demonstração
Edit
|
Attach
|
P
rint version
|
H
istory
:
r8
<
r7
<
r6
<
r5
<
r4
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r6 - 2011-03-22
-
NicolauGrosskopf
WebXEO
XEO Primer
-
Instalation
-
Introduction
-
Concepts
-
Architecture
-
XEO Library
-
Deploy to EAR
-
PreferenceStore
XEO - Core
-
XEO Model Reference
-
Security
-
Java API
-
BOL
-
XEOQL (BOQL)
-
Administrating
-
Background Tasks
-
boConfig.xml
-
Web.xml
-
Known Issues
-
XEO Flags
XEO - XWC
- Web Components
- Java Samples
- Custom Components
- Component Plugins
- Internationalization
- Viewer Events
- Value Change Listeners
- XUIServlet
- XeoLocalization
- XvwTemplates
Create New Topic
WebXEO Web
No permission to view
TWiki.WebTopBar
No permission to view
TWiki.WebBottomBar