0

I made a simple Hibernate 3.1 java code (in eclipse) which successfully creates a table in a database. I have slfj-api-1.6.0.jar and slfj-simple-1.6.0.jar in my project. But, I don't see any Log4j logging warnings or messages in the console output. How do I make log4j log things to my console ?

My tutorial says that one of the lines I should get in the console is -

log4j.logger.org.hibernate.type = debug

I saw a couple of posts online which tell me to place a Log4j.properties file in my java project, either in src folder, or make a resources folder inside src and put it there. I found one Log4j.properties in my computer and placed it in the resources folder. I am not sure if I should use the settings in that file or how to edit this file as per my requirement. How do I do this correctly ?

EDIT -

The log4j.properties file -

### Direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - ###
log4j.rootLogger=warn, stdout log4j.logger.org.hibernate.info

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=debug

###log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

Thanks.

Erran Morad
  • 4,563
  • 10
  • 43
  • 72
  • If memory serves me correctly, `hibernate.cfg.xml` must be in a root folder as well, i.e., a `src` folder. – Buhake Sindi Apr 24 '14 at 21:49
  • @BuhakeSindi - Thanks. I just figured out why my hibernate config file was not being loaded - https://stackoverflow.com/questions/18736594/location-of-hibernate-cfg-xml-in-project/23280572#23280572v Anyway, that is besides the original question. – Erran Morad Apr 24 '14 at 21:56

3 Answers3

0

One thing to note is that the file should be called log4j.properties and should be placed in the root of your src or resource folder (by default at least) ... you seem to be calling it Log4j.properties

I also think that you are missing the following dependency:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.1</version>
    </dependency>
Shiraaz.M
  • 3,073
  • 2
  • 24
  • 40
0

First of all, the file name must be in lowercase. The Log4j.properties file is wrong, it's log4j.properties. This must be put in the root folder of your project (in most cases the src folder).

Secondly, you need to download the log4j jar file if you want to use SLF4J bridge with log4j. Download the latest library that you require from Apache Logging Services. Don't forget that there are 2 versions of Apache Log4J, so I suggest you try version 1.x first.

Good luck!

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • I put it inside `resources` folder which is inside `src` folder. I'll add my logger.properties file. – Erran Morad Apr 24 '14 at 21:31
  • It should be only in `src` folder. That's how Log4J is configured. – Buhake Sindi Apr 24 '14 at 21:48
  • According to @AlexR in this post, you can put it in `src` or in any folder inside `src` - http://stackoverflow.com/questions/5081316/where-is-the-correct-location-to-put-log4j-properties-in-an-eclipse-project – Erran Morad Apr 24 '14 at 22:01
  • I added `log4j 1.2 jar` to my `lib` folder and now I at least get the WARN messages. I tried to add the `log4j.properties` file to my `src` folder also and Log4j gives me the error - `ERROR Could not find value for key log4j.appender.stdout log4j.logger.org.hibernate.info` – Erran Morad Apr 24 '14 at 23:21
  • UPDATE - I am using `Eclipse` to do a `hibernate` example. I DON'T get the WARN messages when I keep `log4j.properties` inside the `src` folder. But, when I keep it under `src/resources` folder, then I get the warning. Why does this happen and how do I make eclipse refer to the correct folder ? – Erran Morad Apr 25 '14 at 00:12
  • 1
    As I said earlier, Log4J looks for `log4j.properties` on the root folder of your project. If you want to add it to your `resources` folder, see this [answer](http://stackoverflow.com/questions/7390521/change-location-of-log4j-properties/7390644#7390644). – Buhake Sindi Apr 25 '14 at 06:45
0

The resources folder is only an option if you identify it as a source folder on the project properties!java build path!source tab.

Slf4j does not use log4j to write to the logs unless you include the slf4j-log4j[version].jar (for example, slf4j-log4j12-1.7.7.jar) jar in your project. If you include this jar, then you must not include the slfj-simple-1.6.0.jar jar in your project.

Here is a link to the slf4j online manual

DwB
  • 37,124
  • 11
  • 56
  • 82
  • I included the slf4j-simple on purpose. A while ago, I got a runtime exception class not found. I searched google and and found that adding slf4j-simple fixes the problem. Honestly, I don't know what I am really doing. Just trying to make learn hibernate. – Erran Morad Apr 24 '14 at 21:07