Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Creating custom appender for log4j(custom appender with osgi fragment plugin for log4j not working)
Creating custom appender for log4j [message #1810097] Tue, 30 July 2019 22:05 Go to next message
shruthi nadig is currently offline shruthi nadigFriend
Messages: 7
Registered: July 2019
Junior Member
Hi All ,
I am trying to create a custom appender by extending org.apache.log4j.FileAppender as part of OSGI fragment eclipse plugin for log4j with log4j.xml file. the log file "/tmp/eclipse.log" is getting created when I use <appender name="fileapppender" class="org.apache.log4j.FileAppender"> in log4j.xml. but the log file is not getting cretaed with custom appender class "DslLogFileAppender". Below is all of my related code.

Can someone please suggest what is wrong here.
package com.visa.ovn.dsl.core.logging

import java.io.IOException
import org.apache.log4j.FileAppender
import org.apache.log4j.Layout
import org.apache.log4j.spi.LoggingEvent
import org.apache.log4j.Logger


class DslLogFileAppender extends FileAppender {

String log_file
boolean initialized = false
val static LOGGER = Logger.getLogger(DslLogFileAppender)
new() {
//this.log_file = System.getProperty("user.home")+"/eclipse-dsl.log"
this.log_file = "/tmp/eclipse.log"

}

new(String log_file, Layout layout) throws IOException {
super(layout,log_file)
}
override append(LoggingEvent event) {
if (!initialized) {
LOGGER.info("create a new log file")
createNewFile()
}
super.append(event)
closeFile();


}

def createNewFile() {
try {
LOGGER.info("set the file name as specified in above constructor")
super.setFile(log_file)
super.activateOptions()
initialized = true;

}catch (Exception e) {
LOGGER.error("Error in configuration of log4j params,unable to create Log file");
}

}
override setFile(String file) {
this.file=file;
}
}
.
MANIFEST.MF looks like this:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Logging
Bundle-SymbolicName: com.visa.ovn.dsl.core.logging;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: VISA
Fragment-Host: org.apache.log4j; bundle-version="1.2.15"
Bundle-Localization: fragment
Export-Package: com.visa.ovn.dsl.core.logging
Require-Bundle: com.google.guava,
org.eclipse.xtext.xbase.lib,
org.eclipse.xtend.lib,
org.eclipse.xtend.lib.macro,
org.eclipse.core.runtime;bundle-version="3.6.0"
Import-Package:
org.osgi.framework;version="1.5.0",
org.eclipse.xtext.logging
Automatic-Module-Name: com.visa.ovn.dsl.core.logging;singleton:=true
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Eclipse-ExtensibleAPI: true
Bundle-ClassPath: target/com.visa.ovn.dsl.core.logging-1.0.0-SNAPSHOT.jar,.


log4j.xml as below:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<param name="ConversionPattern" value="%5p [%t] (%F:%L) - %m%n"/>
</layout>
</appender>
<appender name="eclipse" class="org.eclipse.xtext.logging.EclipseLogAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- Pattern to output to the Eclipse error log -->
<param name="ConversionPattern" value="%c %x - %m%n"/>
</layout>
</appender>
<appender name="fileapppender" class="com.visa.ovn.dsl.core.logging.DslLogFileAppender">
<param name="File" value="/tmp/eclipse.log"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
</layout>
</appender>
<!-- com.visa.ovn.dsl package -->
<logger name="com.visa.ovn.dsl" additivity="false">
<level value="INFO" />
<appender-ref ref="fileapppender" />
<appender-ref ref="stdout" />
</logger>
<root>
<priority value ="WARN" />
<appender-ref ref="stdout" />
<appender-ref ref="eclipse" />
<appender-ref ref="fileapppender" />
</root>
</log4j:configuration>
Re: Creating custom appender for log4j [message #1810099 is a reply to message #1810097] Wed, 31 July 2019 02:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13124
Registered: July 2009
Senior Member
(1) asking the same question in n threads is not helpful
(2) use a debugger
(3) make sure the normal Xtext logging fragment is not present (nor any other fragment for log4j)
(4) make sure you fragment is correct . The bundle classpath and import package look totally suspicious to me


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating custom appender for log4j [message #1810117 is a reply to message #1810099] Wed, 31 July 2019 17:25 Go to previous messageGo to next message
shruthi nadig is currently offline shruthi nadigFriend
Messages: 7
Registered: July 2019
Junior Member
Thank You for your response. Can you please guide me if I can disable only that particular fragment in xtext , if yes how?
Thanks
Re: Creating custom appender for log4j [message #1810118 is a reply to message #1810117] Wed, 31 July 2019 17:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13124
Registered: July 2009
Senior Member
How do you Build / deploy / install your plugin?
If you. Test with run configuration Test can (de)select plugins on the plugins tab


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 31 July 2019 17:50]

Report message to a moderator

Re: Creating custom appender for log4j [message #1810129 is a reply to message #1810118] Wed, 31 July 2019 23:59 Go to previous messageGo to next message
shruthi nadig is currently offline shruthi nadigFriend
Messages: 7
Registered: July 2019
Junior Member
We build and publish through jenkins into the artifactry. Then use install new software option from Eclipse to download the fragment.
Presently am able to create the log file through my custom appender but no log is written into the log file(its a 0B file). I think Fragment is proper as it is able to create file from the custom appender but something is missing in my custom appender implementation. Below is my custom appender code. Please suggest..

import java.io.IOException
import org.apache.log4j.FileAppender
import org.apache.log4j.Layout
import org.apache.log4j.spi.LoggingEvent
import org.apache.log4j.Logger

class DslLogFileAppender extends FileAppender {
String log_file = null
val static LOGGER = Logger.getLogger(DslLogFileAppender)
new() {

super()

}
new(String filename, Layout layout,boolean append) throws IOException {
super(layout,filename,append)
}



override append(LoggingEvent event) {
createNewFile()
this.setFile(log_file)
super.append(event)
closeFile()
}

def createNewFile() {
try {
log_file = "/tmp/eclipse_dsl.log"
LOGGER.info("creating new file")
super.activateOptions();
} catch (Exception e) {

}
}
}
Re: Creating custom appender for log4j [message #1810131 is a reply to message #1810129] Thu, 01 August 2019 04:04 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13124
Registered: July 2009
Senior Member
As is said please follow my suggestions.: please debug log4j. Please check which log4j fragments are present

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 01 August 2019 04:05]

Report message to a moderator

Previous Topic:Validation: Proper way to check for errors in related resources
Next Topic:Scoping same type of objects
Goto Forum:
  


Current Time: Mon Sep 23 03:15:51 GMT 2019

Powered by FUDForum. Page generated in 0.02546 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top