I've looked all over the documentation, but was unable to find one. How can I achieve this?
According to the Audit4j documentation and javadoc there are multiple ways to specify the configuration:
It can be injected into the Context directly by calling static methods of the Context class (e.g. at application startup before doing any audit!):
Context#initWithConfiguration(Configuration configuration) with the appropriate Configuration
or Context#initWithConfiguration(String configFilePath) to specify the configuration as file.
Other ways to specify the path to the configuration file without manually initializing the Context are:
Setting path in environment variable "AUDIT4J_CONF_FILE_PATH"
Setting path in Java system property variable "audit4j.conf.file.path"
Also the configuration file can be put into the application classpath or the user directory. In this case the name of the configuration file has to be "audit4j.conf.yml" or "audit4j.conf.yaml" for YAML style or "audit4j.conf.xml" for XML style.
Related
In my spring boot project, I have env property(application-dev.prop) file placed directly under resources folder, this prop file has some connection properties defined. The same connection properties with different value defined in user.properties file( which needs to be injected in a specific config class)placed under resources/config.I have annotated that specific config class with #PropertySource(value = "classpath:/config/user.properties"). But however this config class takes connection values from env property file but not from user.prop file even if that class annotated with property source user.property class path. This config class only accepts user.property value if a particular field not defined in env property file. In short it checks user.property file only if any field/property not defined in env.property file. But I want this config class to point user.property file always. I already checked spring boot precedence for scanning property file but couldn't resolve. Can anyone help me fix this issue.
As per the doc,
In cases where a given property key exists in more than one .properties file,
the last #PropertySource annotation processed will 'win' and override.
I think a better approach would be to use a profile if that fits your use case.
The link here explains how to add profiles pretty neatly. you can check tht out.
Another approach I would suggest if you think adding a profile is not the right choice, you can make them passed down as environment variables. This gives you flexibility to change it as necessary and you can also give a default value for the environment variables.
Where can i store SLF4J configuration file so that all production and test environment may have access to it? I want to store the config file outside of the web app at an arbitrary location and retrieve that location upon startup. I also want to allow for changing location of the config file so no classpath. Im thinking about using getters and setters to retrieve the file path.
Any ideas??
slf4j is (for all practical purposes) just the API. You need a backend which does the actual work.
If you use logback you can ship a logback.xml file with your application which just includes another file. If I recall correctly the filename string can hold a ${variable} which you can then define outside your application.
See https://logback.qos.ch/manual/configuration.html#configFileProperty
You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
I placed a default log4j2 config file (log4j2.xml) in my application jar.
If the user doesn't define a own config file, I want to load my default config.
I could get a stream of the default config this way:
InputStream defaultConfigStream = MyApp.class.getClassLoader().getResourceAsStream("log4j2.xml");
But that won't help me, because following code takes a full path to the config file to check its existance and to load it.
System.setProperty("log4j.configurationFile", file.toUri().toURL().toString());
How can I accomplish that? I don't want to hardcode my default config settings, like here.
Use a DomConfigurator, see: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html, you can parse the XML and feed it to the configurator and when initializing logger it will automatically consider the configuration
As I found out, it's pretty simple. Like described in the documentation (point 7), Log4j2 will automatically look for a log4j2.xml on the classpath.
So if the user doesn't define a own config file, I don't call any method to load my default config.
I am using log4j to log information in my web application. I have chosen log4j.xml type of configuration instead of log4j.properties. I remember that, for log4j.properties configuration, I did not write Java code lines to find the log4j.property file location. However, for log4j.xml file configuration I am specifying explicitly to find it like this
DOMConfigurator.configure("log4j.xml");//it reads file from classpath, working!
Also, tested my application removing the above statement from source. It doesn't see to work. None of the debug statements were printed except
System.out.println();
I have read that log4j by default looks for log4jproperties file or log4j.xml file in the classpath. I checked in the deployed web application, log4j.xml file is in web-inf/classes.
Even though it can't find log4j.xml.
Is it that, the above code line is mandatory for log4j xml configuration in Java?
In fact, can't Java source code doesn't pickup log4j.xml from the classpath without explicit specification as above?
From log4j manual:
The exact default initialization algorithm is defined as follows:
Setting the log4j.defaultInitOverride system property to any other value
then "false" will cause log4j to skip the default initialization
procedure (this procedure).
Set the resource string variable to the value of the log4j.configuration system
property. The preferred way to specify the default initialization file
is through the log4j.configuration system property. In case the system
property log4j.configuration is not defined, then set the string variable
resource to its default value "log4j.properties".
Attempt to convert the resource variable to a URL.
If the resource variable cannot be converted to a URL, for example
due to a MalformedURLException, then search for the resource from the
classpath by calling org.apache.log4j.helpers.Loader.getResource(resource, Logger.class)
which returns a URL. Note that the string "log4j.properties"
constitutes a malformed URL. See Loader.getResource(java.lang.String) for the list of searched locations.
If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL. The PropertyConfigurator will be used to parse the URL to configure log4j unless the URL ends with the ".xml" extension, in
which case the DOMConfigurator will be used. You can optionaly specify
a custom configurator. The value of the log4j.configuratorClass system
property is taken as the fully qualified class name of your custom
configurator. The custom configurator you specify must implement the
Configurator interface.
So, without code, you can (and should!) define the system property to tell where your config file is. If not, it by default will search for log4j.properties in the classpath. That is the only file it is searching for by default.
System property is usually defined as a startup parameter for your app (app server), something like -Dlog4j.configuration=path/to/your/config.xml.
My java application references a 3rd-party jar file which uses log4j logging. The problem is that this jar contains its own log4j.properties file which causes access denied exceptions on my machine, but I don't have control over the jar file to change its contents.
I have tried adding my own log4j.properties file in my application's classpath, but it doesn't seem to have an effect. If I try to use PropertyConfigurator to import my own settings programmatically, log4j seems to load the jar file's properties file first (causing an exception).
How can I short-circuit log4j to ignore a 3rd-party jar file's log4j.properties file and use my own?
There are several way to override log4j.properties, one of them is:
Use log4j.xml please see the extension
Another approach is:
Setting the log4j.defaultInitOverride system property to any other value
then "false" will cause log4j to skip the default initialization procedure (this procedure).
Set the resource string variable to the value of the log4j.configuration system property. The preferred way to specify the default initialization file is through the log4j.configuration system property. In case the system property log4j.configuration is not defined, then set the string variable resource to its default value "log4j.properties".
Attempt to convert the resource variable to a URL.
If the resource variable cannot be converted to a URL, for example due to a MalformedURLException, then search for the resource from the classpath by calling org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) which returns a URL. Note that the string "log4j.properties" constitutes a malformed URL. See Loader.getResource(java.lang.String) for the list of searched locations.
If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL. The PropertyConfigurator will be used to parse the URL to configure log4j unless the URL ends with the ".xml" extension, in which case the DOMConfigurator will be used. You can optionaly specify a custom configurator. The value of the log4j.configuratorClass system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement the Configurator interface.
Ref: http://logging.apache.org/log4j/1.2/manual.html
If none of these works
Add this following statement in your app configuration
BasicConfigurator.configure();
it worked for me
Try adding an environment variable called LOG4J_CONFIGURATION_FILE while you start the application.