Configure Eclipse for Log4j - log4j

I am working on an application using apache jena, to access dbpedia using sparql. Well I am very new to DBPedia and apache jena, I did some research and started with some sample code to create VCARD.
public class DpPedia extends Object {
static String personURI = "<!..http://def/JohnSmith..!>";
static String fullName = "John Smith";
static String Fullname = "Pulkit Gupta";
public static void main (String args[]) {
// create an empty model
Model model = ModelFactory.createDefaultModel();
// create the resource
Resource johnSmith = model.createResource(personURI);
Resource pulkitgupta=model.createResource(personABC);
// add the property
johnSmith.addProperty(VCARD.FN, fullName);
pulkitgupta.addProperty(VCARD.FN,Fullname);
}
}
However when i tried to execute this code .. It end up with an error
log4j:WARN No appenders could be found for logger (org.apache.jena.riot.stream.JenaIOEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
it seems my eclipse is not configured properly and I need to install Log4j. Well I downloaded the files, but not sure how up configure it with eclipse.

jena use log4j as logging system, and the warning messages tell explicitly that you are lacking of a log4j.properties to initialize it.
"This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration."
Create log4j.properties file under src and retry. You can find some sample config files here

The easiest way to do this is by going to the folder where you extracted apache jena.
Find a properties file named : jena-log4j.properties. Copy this
Navigate to your workspace folder where you have your source project.
Go to bin folder and paste the file there. Make sure you rename the file to just log4j.properties
Clean the project and run it.
The errors would now have gone.

use org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);

Related

log4net is unable to load the file MySql.Data from GAC

I am trying to evaluate the use of log4net for my new Windows Services platform. I was hoping to use generate logs in my MySql database.
I have the latest of MySql (6.9.4.0) and log4net.
I have a create a simple windows console application and to that added a configuration file log4net.config which has my appender information.
Added the following line of code to AssemblyInfo class
[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
and the following in my Program class
private static readonly ILog _debugLogger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
I have both log4net and MySql.Data Gaced in GAC_MSIL and both assemblies added to assemblyBinding section of the Application.config file
When I execute the program I see no logs generated. On turning on Apaches internal logging i see the error that it failed to load the MySql.Data file
Copy MySql.Data file to bin folder and the logs are generated absolutely fine.
Any clues?
Thanks
Regards
Sid
My bad. The connection type element in the appender was missing the complete assembly name. It had MySql.Data.MySqlClient.MySqlConnection, MySql.Data
but should have had
MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.9.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d
Works fine.

ClassLoader - Loading and saving data

Hopefully someone can help me with this.
It is my understanding that using a ClassLoader is the most reliable way to load in content.
public class Pipeline{
public static URL getResource(String filename) {
return ClassLoader.getSystemResource(filename);
}
public static InputStream getResourceAsStream(String filename) {
return ClassLoader.getSystemResourceAsStream(filename);
}
}
If you had a file at "[jar bundle]/resources/abc.png" ..You would load it by:
URL url = Pipeline.getResource("resources/abc.png");
Loading is simple.
Saving is what's getting me.
I have a program that collects data while running, saves that data on exit, and then loads the data back in next time and keeps adding to it.
Easiest solution I think would be to save back into the jar bundle so that ClassLoader can get at them. Is this even possible? Or recommended?
I don't mind having my resources outside of the jar, just as long as I don't have to resort to 'File' to get at them and save to them. (Unless it can be done cleanly)
folder/application.jar
folder/resources/abc.png
If you could ../ back one from where the ClassLoader is looking it would be easy to cleanly get data from the directory that actually contains the jar file
Pipeline.getResource("../resources/abc.png");
Any ideas?
This isn't really what class loaders are meant for. Loading resources from the class loader is meant so that you can bundle up your application as one package and components can read each other without worrying about how the system you're deploying to is setup.
If the file in the JAR is meant to be changed by the app, then it isn't part of the app and thus probably shouldn't be in the JAR.
I don't have a lot of context on your app, but hopefully my suggestion will be valid for your situation.
I recommend setting a requirement in your app that it has a work area to which it is allowed to read and write and accept a configuration setting that specifies where this directory is. Typical ways to do this in Java are with environment variables, system properties or JNDI settings (for container deployments).
Examples:
Tomcat's startup scripts figure out where it is installed and sets a system property called catalina.home and allows you to over-ride it with an environment variable called CATALINA_HOME.
JBoss looks for JBOSS_HOME
Java application servers typically look for JAVA_HOME to find the JDK.

log4j produces no output on specific Weblogic servers

We are distributing an application that is deployed as a bunch of WAR files in various WebLogic servers (9, 10 and 11) at various customers' sites. Those apps are using commons-logging and log4j. It works perfectly everywhere except at a specific site where no logs are generated from our app. That site is on 11g and has a bunch of other Oracle software installed (BPEL, OSB etc)
In our code, we use:
public class Foo {
private final static Log log = LogFactory.getLog(Foo.class);
public void bar () {
log.trace("bar called");
...
Since we want to let the customers adapt their logs configuration, we have a configuration folder in the server CLASSPATH:
CLASSPATH=/opt/app/domain_foo/foo_config/:....
In that folder, two files for logs, commons-logging.properties:
org.apache.commons.logging.Log org.apache.commons.logging.impl.Log4JLogger
And log4j.properties: (this is a simplified one that has the same issue, actual file contains 5 loggers with various parameters)
log4j.rootLogger=INFO, fileApp
# File appender
log4j.appender.fileApp=org.apache.log4j.RollingFileAppender
log4j.appender.fileApp.file=fooapp.log
log4j.appender.fileApp.maxFileSize=10240KB
log4j.appender.fileApp.maxBackupIndex=5
log4j.appender.fileApp.append=true
log4j.appender.fileApp.layout=org.apache.log4j.PatternLayout
log4j.appender.fileApp.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS} %-5p %-70.70C %m%n
log4j.logger.be.foo=DEBUG
The file fooapp.log is created (so it was able to find the properties file) but empty.
I tried to check the log settings in Weblogic but they seem to be the same on working and not working servers.
I copied the CLASSPATH to my server and it is still working fine.
I don't really know where to look anymore. Any ideas ?
It is a bit tricky but the answer is in the environment named: APPLICATIONS_DIRECTORY that should point to the path where APPLICATIONS_DIRECTORY/fooapp.log should be. You can get the directory location from System.getenv("APPLICATIONS_DIRECTORY");

how to configure log4net to work inside a CRM 2011 plugin?

I am trying to log some information inside a CRM 2011 plugin. I am not sure how to configure log4net. Where should I put log4net config file and how to reference from the plugin? Thanks!
Assuming you are registering your plugins to the database, you have a couple options:
Configure log4net programmatically. This can be done via the log4net API and could be driven by a configuration entity in crm.
Embed the log4net config file in the plugin assembly and configure log4net from a stream (shown below in a plugin base class that other plugins who wish to log could inherit from)
namespace TestPlugins
{
public abstract class BaseLoggingPlugin
{
protected static readonly ILog _log = LogManager.GetLogger(typeof(BaseLoggingPlugin));
static BaseLoggingPlugin()
{
using(var config = Assembly.GetExecutingAssembly().GetManifestResourceStream("TestPlugins.log4net.config"))
{
XmlConfigurator.Configure(config);
}
}
}
}
I want to add a warning to whatever the correct answer ends up being:
if you are registering your plugin assembly as a sandbox assembly (sandbox mode is required for CRM-online) you will not have access to the file system. In that case, your only option is Tracing. Good luck!

Can multiple log4j.properties files be used in the same Tomcat web app?

I'm writing a custom extension to an off-the-shelf Java web application. The application uses log4j for logging and I'd like to add a new logger and appender specifically for my extension. The problem is that the application manages the log4j.properties file which is dynamically generated based on selections in an admin screen UI. Since this is an "off-the-shelf" application, I can't modify the source code. So, if I add my own logger & appender(s) to the file, it gets overwritten anytime an admin changes logging preferences in the UI.
Is it possible to get log4j to get it's configuration from 2 files? For example, I'd want something like the following:
applog.properties #(Dynamically generated from admin UI)
mylog.properties #(My static properties)
In this scenario, log4j would somehow combine the entries from both files for the complete configuration.
Is this possible? or are there other workarounds?
I never did find a way to "merge" multiple log4j.properties file, but I did find a workable solution. log4j configuration can be manipulated programatically at runtime similar to the code snippet below. This effectively merged my custom log4j settings into the configuration defined by the log4j.properties file, which in my case I couldn't edit.
// Init custom logging
// Define layout
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("%d [%-5p] -- %m%n");
// Create appender
RollingFileAppender appender = new RollingFileAppender();
appender.setFile(LOG_PATH);
appender.setMaxFileSize("2MB");
appender.setMaxBackupIndex(0);
appender.setLayout(layout);
appender.activateOptions(); // It didn't work without this
// Get our logger and add appender.
log = Logger.getLogger("[MyCustomLogger]");
log.setLevel(YOUR_LOGGING_LEVEL_HERE);
log.addAppender(appender);

Resources