1

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

LMG
  • 966
  • 1
  • 12
  • 28
  • hey check this one hope [this][1] will help you [1]: http://stackoverflow.com/questions/14182257/using-applications-log4j-configuration-under-jboss-7-1-1 – Ajay Mirge Jan 22 '13 at 12:14
  • ok guys, i tried for 2 hours but it seems is not working.. any other suggestion? – LMG Jan 22 '13 at 15:12

1 Answers1

2

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"):
<periodic-rotating-file-handler name="FILETWO">
  <formatter>
    <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
  </formatter>
  <file relative-to="jboss.server.log.dir" path="myappfile.log"/>
  <suffix value=".yyyy-MM-dd"/>
  <append value="true"/>
</periodic-rotating-file-handler>
  • 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):
<logger category="com.mypackage">
    <level name="DEBUG"/>
    <handlers>
        <handler name="CONSOLE"/>
        <handler name="FILETWO"/>
    </handlers>
</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.

Rafael Sisto
  • 404
  • 5
  • 19
  • Ty rafael for responding. I'm using jboss as 7.1.1 and in the jboss folder there is no folder "config" with "jboss-log4j.xml", if i want to use "import org.apache.log4j.*" i should add to the classpath its relative jar hence it would be in conflict with the supposed already present log4j, can you please be kind and be more clear in your explanation (which i appreaciate a lot since is the more comprehenisble since now of all of those i've seen). Thanks – LMG Jan 22 '13 at 20:13
  • No problem. For the import, if you are using eclipse, just add the runtime environment to the classpath (project properties -> Java Build Path -> Libraries -> Add Library -> Server Runtime -> your_server). If you don't have the server configured, just add the log4j.jar to the classpath (present in the %JBOSS_PATH%/common/lib/. – Rafael Sisto Jan 23 '13 at 12:42
  • I am basing my info from a jboss 5 server, but it seems it is not available in jboss 7.. Let me dig a little deeper and I'll get back. – Rafael Sisto Jan 23 '13 at 12:49
  • Yes, this won't work on JBoss AS 7.1.x. If you're not using any appenders other than console or some file appender, you can configure this with the logging subsystem and not have to worry about including a log4j configuration. Otherwise see this http://stackoverflow.com/questions/9584787/using-log4j-with-jboss-7-1/9589133#9589133 – James R. Perkins Jan 23 '13 at 16:57
  • dear James, i tryied but is not working! it reads the configuration file but it does not write to it.. i don't know anymore what to do.. i added that configuration file to exclude the org.apache.bla but is not doing anything new.. i go MyLogFile.log created, in the place i specificated, but is empty (it means the configuration file has been read).. – LMG Jan 23 '13 at 17:19
  • I tried it locally and it worked and edited the post accordingly. – Rafael Sisto Jan 24 '13 at 13:02
  • 1
    @Rafael GREAT! it seem to work good! Thank you very much indeed! i'll build a post for those who needs it and cite you on [java for the web](http://javafortheweb.blogspot.it/) blog.. – LMG Jan 25 '13 at 10:43
  • Cool, it is also in my blog: [link](http://gauchoacomecable.wordpress.com/). I was just reading and saw that u guys are from Trento, my grandpa is from there and came to Uruguay and my uncle lives actually there :) – Rafael Sisto Jan 25 '13 at 20:21
  • Hey, feel free to post it in your blog also, the more the merryer :) – Rafael Sisto Jan 25 '13 at 20:29