Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
mule_logging [2018/12/17 08:08]
root [Mule Anypoint configuration]
mule_logging [2019/02/08 05:50] (current)
root
Line 39: Line 39:
 Just replace "​tcp"​ or "​udp"​ Just replace "​tcp"​ or "​udp"​
  
 +
 +== Configure Log4j2 with an JDBC Appender in Mule ==
 +
 +The steps for implement this solution to any project are
 +* Add sqljdbc4.jar to buildpath.
 +* Add the ConnectionFactory.java to your project.
 +* The ConnectionFactory class is very simple:
 +<code java>
 +private static interface Singleton {
 +  final ConnectionFactory INSTANCE = new ConnectionFactory();​
 +}
 +
 +private final DataSource dataSource;
 +
 +private ConnectionFactory() {
 +  SQLServerDataSource ds = new SQLServerDataSource();​
 +  ds.setServerName(System.getProperty("​url"​));​
 +  ds.setPortNumber(Integer.parseInt(System.getProperty("​dbPort"​)));​
 +  ds.setDatabaseName(System.getProperty("​databaseName"​));​
 +  ds.setUser(System.getProperty("​user"​));​
 +  ds.setPassword(System.getProperty("​password"​));​
 +  this.dataSource = ds;
 +}
 +
 +public static Connection getDatabaseConnection() throws SQLException {
 +   ​return Singleton.INSTANCE.dataSource.getConnection();​
 +}
 +</​code>​
 +
 +Modify log4j.xml with you prefered database/​table configuration and point to connection factory java class and method.
 +
 +<​code>​
 +<Jdbc name="​databaseAppender"​ tableName="​dsLogEntry"​ ignoreExceptions="​false"​ >
 +  <​ConnectionFactory class="​net.example.db.ConnectionFactory"​ method="​getDatabaseConnection"​ />
 +  <Column name="​eventDate"​ isEventTimestamp="​true"​ />
 +  <Column name="​literalColumn"​ literal="'​Literal Value of Data Source'"​ />
 +  <Column name="​level"​ pattern="​%level"​ />
 +  <Column name="​logger"​ pattern="​%logger"​ />
 +  <Column name="​message"​ pattern="​%message"​ isUnicode="​false"​ />
 +  <Column name="​exception"​ pattern="​%ex{full}"​ />
 +</​Jdbc>​
 +</​code>​
 +
 +Also the appender ref in the log4j.xml must be pointing to the jdbc appender.
 +
 +<​code> ​
 +<​AppenderRef ref="​databaseAppender"​ level="​INFO"/>​
 +Create the logging table in the running database e.g:
 +CREATE TABLE [dbo].[dsLogEntry](
 +  [id] [numeric](18,​ 0) NOT NULL,
 +  [eventDate] [datetime] NULL,
 +  [literalColumn] [varchar](5000) NULL,
 +  [level] [varchar](5000) NULL,
 +  [logger] [varchar](5000) NULL,
 +  [message] [varchar](5000) NULL,
 +  [exception] [varchar](5000) NULL
 +) ON [PRIMARY] ​
 +</​code>​
 +
 +The last step is configure the VM arguments in the Mule Application Server and run it.
 +For studio Execute the application with the next parameters for the JVM:
 +<​code>​
 +-Durl=localhost -DdbPort=1433 -DdatabaseName=datatest -Duser=testuser -Dpassword=test123
 +</​code>​
 +
 +For Standalone
 +Follow the next guideline How-can-I-set-Mule-and-Java-system-properties-at-startup to configure the next parameters
 +<​code>​
 +url=localhost ​
 +dbPort=1433 ​
 +databaseName=datatest ​
 +user=testuser ​
 +password=test123
 +</​code>​
 
mule_logging.txt · Last modified: 2019/02/08 05:50 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd