2

I am trying to output Selenium logs test to mysql database. I have logback.xml that is located in src of package:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
                <dataSource class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
            <serverName>http://php4dvd.com.ua/openserver/</serverName>
            <port>3306</port>
            <databaseName>ConsoleOutput</databaseName>
            <user>root</user>
            <password></password>
                </dataSource>
        </connectionSource>
</appender>
        <root level="DEBUG" >
                <appender-ref ref="DB" />
        </root>
</configuration>

and my JUnit test is:

package us.st.selenium.protocols;

import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import ch.qos.logback.core.db.ConnectionSource;
import ch.qos.logback.classic.db.DBAppender;

public class ConsoleOutputToMySql {

private RemoteWebDriver driver;

    private static Logger LOG = LoggerFactory.getLogger(ConsoleOutputToMySql.class);

    @Before
        public void initDriver(){
        LOG.debug("Starting Firefox");
        driver = new FirefoxDriver();
        LOG.debug("Firefox started");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void sampletest() throws Exception {
        LOG.info("Started sampletest");
        LOG.info("Go to main page");
        driver.get("http://php4dvd.com.ua");
        LOG.info("login as admin / admin");
        driver.findElement(By.id("username")).sendKeys("admin");
        driver.findElement(By.name("password")).sendKeys("admin");
        driver.findElement(By.name("submit")).sendKeys(Keys.RETURN);
        Thread.sleep(4000);

        LOG.info("logout");
        driver.findElement(By.xpath("//header//li[4]/a")).click();
        driver.switchTo().alert().accept(); 
        LOG.info("Finished sampleTest");
    }

    @After

    public void stopDriver(){
        LOG.debug("Firefox finished");
        driver.quit();
    }

}

I have already created db with mysql fields. I can get access to db table by this link on my local machine:

http://php4dvd.com.ua/openserver/phpmyadmin/index.php?db=ConsoleOutput

here is repository of test: https://github.com/Arkhypov/Selenium_tests/tree/master/SeleniumIntermediate/src

When I run tests I still see console logging output instead of having logs appended to db. I think I forgot something... Who can help, please?

Yu Zhang
  • 2,037
  • 6
  • 28
  • 42
Yevgen Arkhypov
  • 53
  • 2
  • 12

1 Answers1

2

ch.qos.logback.core.db.DriverManagerConnectionSource does not have a dataSource property whereas ch.qos.logback.core.db.DataSourceConnectionSource does. You probably want to configure DB with it like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
                <dataSource class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
            <serverName>http://php4dvd.com.ua/openserver/</serverName>
            <port>3306</port>
            <databaseName>ConsoleOutput</databaseName>
            <user>root</user>
            <password></password>
                </dataSource>
        </connectionSource>
</appender>
        <root level="DEBUG" >
                <appender-ref ref="DB" />
        </root>
</configuration>

Or you can use JDBC driver instead of MysqlDataSource.

<configuration>

  <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
    <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
      <driverClass>com.mysql.jdbc.Driver</driverClass>
      <url>jdbc:mysql://host_name:3306/datebase_name</url>
      <user>username</user>
      <password>password</password>
    </connectionSource>
  </appender>

  <root level="DEBUG" >
    <appender-ref ref="DB" />
  </root>
</configuration>

Also, Logback cannot create tables itself. You need to create both database and its tables. You can get create script from here.

Update

In case your logback.xml is not in class path, you can specify the log file you are using while starting your application with property logback.configurationFile . Here is an example

 java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1

In eclipse you can define this kind of parameters in Run Configuration. You can find details here

Update 2

According to your github project you added 2 slf4j-api implementation on your class path. You can remove slf4j-simple-1.7.12.jar. And still use Logger like this.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyService {
    private static final Logger logger = LoggerFactory.getLogger(MyService.class);
}

Update 3

I also created a maven project in github, that demonstrate how to append log to database (postgresql) using logback and slf4j. You can get it from github.

Community
  • 1
  • 1
bhdrkn
  • 6,244
  • 5
  • 35
  • 42
  • Hi, Thank you for your respond. But unfortunately changing to did not help me. I think I should specify path to logback.xml in my java code, no? – Yevgen Arkhypov Sep 01 '15 at 15:22
  • @YevgenArkhypov According to your GitHub project, It seems that logback.xml is in your classpath. And It should be sufficient to be loaded. You can be sure by checking very first line of your console log. It should say something about finding logback.xml on your class path and loading DB as an appender. – bhdrkn Sep 01 '15 at 19:56
  • no it does not. It says: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/auto_tools/slf4j-1.7.12/slf4j-simple-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/auto_tools/logback-1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory] [main] INFO us.st.selenium.protocols.ConsoleOutputToMySql - Started sampletest ... – Yevgen Arkhypov Sep 01 '15 at 20:32
  • @YevgenArkhypov you do not need the slf4j-simple-1.7.12.jar on your class path. You already have Logback. But you are correct, It seems logback.xml is not evaluated. Do you try to specify logback.configurationFile property? – bhdrkn Sep 02 '15 at 05:41
  • @ bhdrkn, I misunderstood you: 1) after removing slf4j-simple-1.7.12.jar from my build path, I could not specify "Loggerfactory": `private static Logger LOG = LoggerFactory.getLogger(ConsoleOutputToMySql.class);` 2) It seems I added logback.xml by `static { System.setProperty("logback.configurationFile", "D:/Selenium_2.0/Selenium_tests/SeleniumIntermediate/src/logback.xml"); } public void main (String[] args) { }` - is this correct? – Yevgen Arkhypov Sep 03 '15 at 04:52
  • @YevgenArkhypov slf4j-simple-1.7.12 is an implementation of slf4j-api. Logger and LoggerFactory classes are came from slf4j-api. You may use wrong classes. And about parameters, setting it after application start will not work. You need to pass it from Run Configuration in eclipse. I updated my answer for both situations. – bhdrkn Sep 03 '15 at 06:06
  • 1
    @ bhdrkn, your updated logback.xml and deleting slf4j-simple-1.7.12.jar from build path eventually helped me! Thank you very much!!! – Yevgen Arkhypov Sep 04 '15 at 04:23