本文介绍了你能解释一下如何在Jboss7.1的ejb模块中使用log4j吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以给我一个一步一步的程序来跟踪,以使log4j工作在ejb模块上?
的情况是这样的:我有我的ejbmodule,这是使用hibernate执行一些提取任务,我想记录一切。我想使用log4j,但是我不能理解网络上描述的其他程序的一些步骤,像jboss那样。



你能告诉我我应该做什么,一步一步?



就我而言:




  • 我有我的配置文件(我不知道该放在哪里)


  • 我有log4j.jar(我不知道在哪里放置)


  • 我不知道如何初始化日志环境(我有一个ejb会话bean,在启动时初始化hibernate,我想一个用于初始化记录器)




任何人都可以帮助我吗?



提前感谢



编辑:
感谢拳头建议,它可能有帮助,但我不会不知道别人怎么可能有META- INF在耳边。
我的项目看起来像3个项目:




  • MyprojectLogic(其中包含ejbmodules)

  • MyprojectLogicClient(它包含所有的接口,它是给客户端的库)

  • MyprojectLogicEAR(实际上没有包含任何东西,我没有得到它的目的是什么) li>


ANSWER:见下文或查看这里或

解决方案

Jboss已经带有log4j。
开发EJB时需要:


  1. 导入Logger类: import org.apache。 log4j.Logger;

  2. 如果导入未解决,请将此jar添加到路径:%JBOSS_HOME%\modules\org\apache\ log4j\main\log4j-1.2.16.jar

  3. 使用记录器创建一个字段: private static Logger logger = Logger.getLogger(YourEJBClass.class) ;

  4. 以任何方式使用记录器: logger.info(my log); / li>

JBoss已经附带了一个配置文件,位于%JBOSS_FOLDER%/ standalone / configuration / standalone.xml 。默认情况下,您的记录器将输出到server.log和控制台,但您可以添加一些appender以将应用程序日志输出到另一个文件。



以下是一个示例,编辑该文件并添加以下代码段(遵循文件定义):




  • 这个是在日志文件夹中的文件myappfile.log中添加一个appender(在已经定义的名称=FILE之前添加它):






  • 添加此代码段告诉log4j将所有日志从com.mypackage类别发送到先前创建的appender(在文件中的其他定义的记录器之后但在根记录器之前复制此权限):



重新启动服务器,您应该看到新的myappfile.log文件夹。确保你的应用程序记录一些东西,例如,在一些servlet中添加logger.info(我的servlet信息日志),并从浏览器中调用它。您还将在控制台中看到此日志



如果您需要进一步的自定义,请查看log4j帮助。


can you give me please a step by step procedure to follow, in order to make log4j work on an ejb module?the situation is this: i have my ejbmodule, which is using hibernate to perform some fetching tasks, and i want to log everything. I want to use log4j but i can not understand some steps of the others procedure described on the web like those from jboss.

Can you please tell me what i should do, step by step?

As far as i am:

  • I have got my configuration file (and i don't know where to put it)

  • I have got the log4j.jar (and i don't know where to place it)

  • I don't know how to initialize the log environment (i have an ejb session bean which on startup initializes hibernate, i would like one to initialize the logger)

Can anyone of you help me please?

Thanks in advance

EDIT:thanks for the fist suggestion it might help but i don't undestand how others may have META-INF in the ear.My project looks like 3 projects togheder:

  • MyprojectLogic (which contains the ejbmodules)
  • MyprojectLogicClient (which contains all the interfaces, it is the library to be given to the client)
  • MyprojectLogicEAR (which actually contains nothing, i did not get what is its purpose)

ANSWER: see below or check here javafortheweb or blog

解决方案

Jboss already comes with log4j. When developing your EJB you need to:

  1. import the Logger class: import org.apache.log4j.Logger;
  2. If the import does not resolve, add this jar to the path: %JBOSS_HOME%\modules\org\apache\log4j\main\log4j-1.2.16.jar
  3. create a field with the logger: private static Logger logger = Logger.getLogger(YourEJBClass.class);
  4. use the logger in any method: logger.info("my log");

JBoss already comes with a configuration file under %JBOSS_FOLDER%/standalone/configuration/standalone.xml. By default, your logger will output to server.log and console, but you can add some appender to output your app logs to another file.

Here is an example, edit that file and add the following snippets (following the definition of the file):

  • this one is to add an appender to the file myappfile.log inside the logs folder (add this right after the already defined under name="FILE"):

  • add this snippet tells log4j to send all logs from "com.mypackage" category to the previously created appender (copy this right after other defined logger in the file, but before the root-logger):

Restart the server and you should see the new myappfile.log in the folder. Make sure your app logs something, for example, adding logger.info("my servlet info log") in some servlet and call it from the browser. You'll also see this log in the console

If you need further customization, have a look at log4j help.

这篇关于你能解释一下如何在Jboss7.1的ejb模块中使用log4j吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:43