2

Is it possible, when using Log4Cxx, to write the logs to MS SQL server? I have been Googling for a while and have found a few examples that use the ODBCAppender. E.G.:

<appender name="MyOdbcMysqlAppender" class="org.apache.log4j.odbc.ODBCAppender">
    <param name="URL" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=errorlog;User=logger;Password=abc123;Option=3;"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="INSERT INTO errorlog (errormessage) VALUES ('%d - %c - %p - %m')"/>
    </layout>
</appender> 

Maybe I just need the correct value for the 'Driver' field? I am using MS SQL Server 2008.

Thank you!

ninjaPixel
  • 6,122
  • 3
  • 36
  • 47

1 Answers1

0

I've gotten the following to work; define an ODBC source named LPErrorLog, define a login 'logger' with some password(abc123), configure SQLServer to support both WIndows and SQLServer authentication. Test your ODBC source, make sure you can login and access the target DB (LPErrorLog in example below) configure your ConversionPattern to match the fields in your DB. Temporarily configure same ConversionPattern for a normal file appender, then scarf the resulting INSERT INTO string from the file after logging to it, drop it into a SQL Server Management Studio script, then execute it to verify that it inserts what you want into the target DB, that it succeeds.

<appender name="MySqlAppender" class="org.apache.log4j.odbc.ODBCAppender">
 <param name="URL" value="DSN=LPLogStore;Server=localhost\SQLEXPRESS;Database=LPErrorLog;Uid=logger;Pwd=abc123"/>
 <layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="INSERT INTO [LPErrorLog].[dbo].[errorlog] ([Logger],[LogTime],[Level],[FileName],[Location],[LineNo],[Message]) VALUES ('%c','%d{dd MMM yyyy HH:mm:ss,SSS}','%p','%F','%l','%L','%m')" />
</layout>

frediano
  • 31
  • 4