The Blog http://blog.jyore.com/?p=234
Introduction
When it comes down to it, logging is one of the most important parts of an application. Logging, when done correctly, allows us to monitor code behavior and find bugs for example.
JBoss has its own implementation of the popular logging frameworks [log4j, apache commons, slf4j] that it lumps into a package called JBoss logging. The application platform utilizes a logging subsystem that manages your logs, instead of the traditional log4j.properties/log4j.xml files that the vast majority of Enterprise Java developers are acquainted with. So, how can logging be configured to accomplish the logging behavior we desire? Is it possible to still use a log4j.properties? The short answer is read on and you will be able to correctly work with JBoss Logging, or if you want a traditional approach, then you will learn how you can bypass JBoss Logging and work with your own configuration as well.
This article will take a look at two logging strategies:
- Logging with JBoss Logging
- Logging without JBoss Logging [Traditional Logging Approach]
Logging with JBoss Logging
At the core, JBoss logging is log4j, so if you are familiar with log4j configuration, it should be a relatively easy transition to the new JBoss Logging. The main difference, other than syntax of course, is that the appenders from log4j are handlers in JBoss.
So, why convert to using the JBoss Logging, especially considering the next sections show how to avoid using JBoss Logging? Well, the short answer is, your logging becomes much easier to manage and interact with. JBoss logging configuration goes into the standalone/domain xml files as all EAP 6 and AS 7 configuration does. The web console and the command line interface (CLI) tools both give you the ability to create and manage your logs. What’s more is, most all logging configuration happens on the fly, which means, you can crank your logs down to DEBUG to capture more information on an error state and turn it back to INFO without having to bounce your server.
Log Handlers
The log handlers are the appenders of JBoss logging. You can add a console, periodic rotating file, and size rotating file handlers. Each type (covered below) has some specific configuration required, some optional, and some that default to certain values if not set.
Console Handler
The console handler is used for logging out to the console as the name suggests. The configuration for a console handler is pretty straight forward.
Property | Required | Default | Description |
autoflush | No | true | Automatically flush after each write |
encoding | No | undefined | Character encoding to use |
filter | No | undefined | Filter to use |
formatter | No | %d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n | Defines a formatter |
level | No | INFO | The log level to set the handler to |
target | No | System.out | The target output |
A command line example for adding a default console handler is as follows:
Which would correspond to the following XML in the server xml configuration:
Another example that changes some of the defaults is as follows:
/subsystem=logging/console-handler=MyConsoleHandler2:add(autoflush=true,level=DEBUG,formatter="%-5p %d{yyMMdd HHmmss,SSS} %X{sessId} %c: %m%n")
<console-handler name="MyConsoleHandler2" autoflush="true">
<level name="DEBUG"/>
<formatter>
<pattern-formatter pattern="%-5p %d{yyMMdd HHmmss,SSS} %X{sessId} %c: %m%n"/>
</formatter>
</console-handler>
Periodic Rotating File Handler
Size Rotating File Handler
Traditional Logging Approach
So, let’s say you decide that JBoss Logging is not for you, and there are a number of reasons that you may decide this. If you strive to keep your code container agnostic and would rather keep a traditional approach that will work on other platforms is one of the best reasons. Or, if you are migrating your application from a previous version of JBoss, WebLogic, WebSphere, etc to EAP 6, for example, then you may not want to reconfigure all of your logging. Maybe you just don’t like the container controlling your Logging. Whatever the reason, you can pretty easily get your logging configuration working in a couple steps.
NOTE: The following will go over the basic steps FIRST, which will apply to just single deployments. EAR deployments are slightly different in that it expands upon the single deployment steps and are covered in the last section below.
Disable JBoss Logging
In this step, the JBoss Logging jars are removed from the application. The JBoss jar’s have their own Log Manager which will not correctly pick up and will not use your logging configuration. In fact, you will likely get errors without this step.
Disabling JBoss Logging is done on an application basis in the jboss-deployment-structure.xml file. This does allow for you to have some apps using JBoss Logging and some using their own Log Manager, however, I strongly suggest using one strategy or another for all applications.
As before, we will stick with Log4j and Apache Commons Logging, however, you can use SLF4j or similar with the same basic approach.
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="org.apache.log4j"/>
<module name="org.apache.commons.logging"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
If you are using an EAR, then please skip to the section below discussing EAR deployments. If you are using a WAR, then this file is placed in your WEB-INF directory.
Package the Logging JARs and Configuration
You will now package your logging JARs inside of your application. This would be inside your lib directory of your EAR or your WEB-INF/lib directory of a WAR as normal.
You will also place your log4j.properties/log4j.xml on your classpath as normal.
Add a JAVA_OPT to Startup
Now, when starting the application server, you will need to add a JAVA_OPT. This flag will make sure that the JBoss Log Manager does not pick up your logging configuration and your own logging JARs will work as normal.
./standalone.sh -Dorg.jboss.as.logging.per-deployment=false
And now your application will now log using the packaged JARs, effectively bypassing JBoss Logging.
EAR Deployments
Now, the above example is all fine and good if you are deploying a WAR, however, Enterprise applications typically have multiple deployments packaged in an EAR. If you try the above method, then the JBoss Log Manager will ignore your configuration and use the containers logging subsystem.
For EAR packages, the jboss-deployment-structure.xml, first of all be in the EAR’s META-INF directory. Additionally, you must add exclusions for the deployment AND each sub-deployment. The logging jars should be in the EAR/lib directory. You should create a module that you can use to bring in the log4j.properties file so all sub-deployments can reference it. Finally, you must start with the JAVA_OPT as noted above.
Example jboss-deployment-structure.xml file:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment name="logging-war-1.0.war">
<dependencies>
<module name="com.jyore.logging" export="true"/>
</dependencies>
<exclusions>
<module name="org.apache.log4j"/>
<module name="org.slf4j"/>
</exclusions>
</deployment>
<sub-deployment name="logging-war-1.0.war">
<exclusions>
<module name="org.apache.log4j"/>
<module name="org.slf4j"/>
</exclusions>
</sub-deployment>
</jboss-deployment-structure>
Example module.xml:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.jyore.logging">
<resources>
<resource-root path="."/>
</resources>
<dependencies>
<module name="org.apache.log4j"/>
</dependencies>
</module>
Example module layout:
EAP_HOME
`-- modules
`-- com
`-- jyore
`-- logging
`-- main
|-- module.xml
`-- log4j.properties
I included a project and the module for your own testing. Enjoy!
Downloads: logging-example.zip | modules.zip
相关推荐
在使用jBoss Enterprise Application Platform (EAP) 6.2 或 jBoss Application Server (AS) 7及以上版本的过程中,可能会遇到字符编码问题,即所谓的“乱码”现象。这种现象通常出现在应用程序的日志、输出数据或与...
Java编程中的`java.lang.NoClassDefFoundError: org/jboss/logging/`是一个常见的运行时错误,通常发生在尝试执行一个类时,JVM无法找到在编译时已经存在的类定义。这个错误并不意味着类在编译期间不存在,而是表明...
本篇将详细介绍如何在MyEclipse中远程调试JBoss AS7或JBoss EAP6,无论是在Windows还是Linux环境下。 首先,我们需要了解远程调试的基本原理。远程调试通常依赖于Java的调试接口(Java Debug Wire Protocol, JDWP)...
JBoss Enterprise Application Platform (EAP) 是 Red Hat 提供的一款开源中间件,用于构建、部署和管理企业级 Java 应用程序。JBoss EAP 7.2.6 版本是一个重要的更新,包含了多个版本的 GA(General Availability)...
JBoss EAP 6.4 是一款广泛应用的企业级应用服务器,尤其适合部署Web应用程序。本文主要介绍了如何配置和管理JBoss EAP 6.4,包括安装和部署FineReport、修改服务器端口、调整内存设置以及改变Web工程的根目录。 ...
JBoss Enterprise Application Platform (EAP) 6 是一个基于Java EE 6的开源应用服务器,为企业级应用程序提供了一个稳定、安全的运行环境。在Windows平台上部署JBOSS EAP6涉及多个步骤,包括安装Java Development ...
在Linux环境下,搭建JBoss Enterprise Application Platform (EAP)的集群能够提高应用程序的可用性和可扩展性。JBoss EAP 6.4.0提供了两种运行模式:standalone(独立运行模式)和domain(域模式)。standalone模式...
JBoss EAP(Enterprise Application Platform)是Red Hat公司推出的一款开源、基于Java EE(现在称为Jakarta EE)的应用服务器,它为企业级应用程序提供了稳定、安全和可扩展的运行环境。版本7.2.0是该平台的一个...
JBoss EAP 6.4 是 Red Hat 提供的一个企业级应用服务器,它基于 Java EE 6 规范,提供了全面的中间件服务,用于构建、部署和管理企业级应用程序。这个版本是 JBoss 产品线的一个关键里程碑,因为它包含了众多功能...
JBoss Enterprise Application Platform (EAP) 是Red Hat公司推出的一款基于Java EE(Enterprise Edition)规范的开源应用服务器,它提供了全面的企业级应用程序开发、部署和管理解决方案。在6.2.0版本中,JBoss EAP...
JBoss EAP(Enterprise Application Platform)6.1是Red Hat公司推出的一款企业级Java应用服务器。作为JBoss应用服务器的商业版,EAP 6.1提供了生产级别的支持、性能优化、安全性提升等特性。该平台基于Java EE标准...
JBoss EAP 6.3.0 是一个企业级的应用服务器,由Red Hat公司开发,是Java EE 6规范的实现。它提供了丰富的功能和工具,用于构建、部署和管理基于Java的应用程序。以下是关于JBoss EAP 6.3.0的一些关键知识点: 1. **...
JBoss EAP(Enterprise Application Platform),是Red Hat公司开发的一款基于Java EE(现在称为Jakarta EE)的应用服务器,它为开发者提供了一个全面的框架来构建、部署和管理企业级应用程序。标题中的"jboss-eap-...
JBoss EAP (Enterprise Application Platform) 是一款由 Red Hat 提供的企业级 Java 应用服务器,它基于开源项目 WildFly 构建而成,提供了丰富的功能和服务,适用于构建、部署和管理企业级应用程序。在实际生产环境...
首先,JBoss EAP(Enterprise Application Platform)是基于开源项目WildFly的商业版本,它包含了对Java EE 7规范的支持。这意味着开发者可以利用EAP 7.1.0来开发符合Java EE标准的Web应用、企业级服务、数据访问...
### Linux JBoss EAP集群搭建详解 #### 一、引言 在生产环境中,为了提高应用程序的可用性和可扩展性,通常会选择将应用部署在JBoss EAP集群中。JBoss EAP支持两种运行模式:standalone(独立运行模式)与domain...
jboss_EAP 部署在Window平台上
DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd"> <jboss-web> <!-- For load class independently --> ...
- **日志系统**:JBoss EAP使用Log4j或JBoss Logging记录日志,便于调试和问题排查。 - **单元测试与集成测试**:支持JUnit、Arquillian等测试框架,确保代码质量。 6. **性能监控与调优**: - **JMX(Java ...