Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7179497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:09:07+00:00 2026-05-28T17:09:07+00:00

It’s been a while that I began with log4j; pretty cool logging framework. I’ve

  • 0

It’s been a while that I began with log4j; pretty cool logging framework. I’ve done other type of logging like Console and File Logging. So trying for DB Adapters with mysql for Database logging. Accordingly, I’ve created following property file named log4j.properties as –

# Define the root logger with appender file
log4j.rootLogger = DEBUG, DB

# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender

# Set JDBC URL
log4j.appender.DB.URL=jdbc:mysql://localhost:3306/test

# Set Database Driver
log4j.appender.DB.driver=com.mysql.jdbc.Driver

# Set database user name and password
log4j.appender.DB.user=root
log4j.appender.DB.password=

# Set the SQL statement to be executed.
log4j.appender.DB.sql=insert into log(date,level,message) values("%d","%p","%m")

# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

And used it in a test class in following way –

 public class DBLoggerTest {
    static Logger logger;

    public DBLoggerTest() {
        //System.setProperty("log4j.configuration", "log4j.properties");
        logger = Logger.getLogger(DBLoggerTest.class.getName());

    }

    public static void main(String[] args) {
        new DBLoggerTest();
        logger.info("This is a test info");
        logger.error("This is an error messsage");
    }
}

But I got following error –

log4j:WARN No appenders could be found for logger (com.satyam.logger.test.DBLoggerTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Any help please…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:09:08+00:00Added an answer on May 28, 2026 at 5:09 pm

    If you are using mysql. create a file log4j.properties. This worked for me.
    Put this at the root folder of you application. i.e root of all packages. I also do have a table logs with fields id,date,user, message and class.

    log4j.rootLogger=DEBUG,DB
    log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
    log4j.appender.DB.URL=jdbc:mysql://localhost:3306/test
    log4j.appender.DB.user=root
    log4j.appender.DB.password=root
    log4j.appender.DB.sql=INSERT INTO logs(date, user, message,class) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%X{User}','%m','%c')
    log4j.appender.DB.layout=org.apache.log4j.PatternLayout
    log4j.appender.CA.layout.ConversionPattern=INSERT INTO logs (date, user,message,class) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%X{User}','%m','%c')
    
    log4j.category.ke.co=ERROR
    log4j.category.ke.co.appender-ref=DB
    

    Then use it as follows.

    package com.zeddarn;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import org.apache.log4j.Logger;
    import org.apache.log4j.MDC;
    
    public class MySQLDatabaseConnector {
    
    static ThreadLocal<Connection> connection = new ThreadLocal<Connection>();
    private static Logger logger = Logger.getLogger(MySQLDatabaseConnector.class);
    
    public static Connection getDBConnection() {
    
        //check if a mysql connection already exits. This is to avoid reconnecting 
       if (connection.get() == null) {
            try {
                //loading the mysql driver. This means you also have to add mysql libary. You can add manually or via maven
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                //do something to deal with the error of missing mysql driver e.g notification to the user.
                 MDC.put("User", "loggeduser");
                logger.error(e.getMessage());
                MDC.getContext().clear();
            }
            try {
    
                connection.set(DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"));
    
            } catch (SQLException e) {               
                MDC.put("User", "loggeduser");
                logger.error(e.getMessage());
                MDC.getContext().clear();
          }
        }
        return connection.get();
    }
    
    public static void main(String args[]) {
          MDC.put("User", "loggeduser");
                logger.error("message from exception.getMessage() method");
                MDC.getContext().clear();
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.