I am using log4j 1.2 with commons-logging. Now I am trying to upgrade it to log4j2.
But how to use log4j2 with commons-logging to initialize log4j2.
I tried to initialize commons logging in the below way. Its working fine
**Statement1**: static Log log = new Log4JLogger(Logger.getLogger(Example.class));
**Statement2**:log.debug("debug statement");
Here I am using object of type org.apache.commons.logging.Log initialized with object of org.apache.log4j.Logger.(org.apache.log4j.Logger is the class from log4j 1.2 where as from log4j2 is changed to org.apache.logging.log4j.Logger)
Now after I upgrade to log4j2, Statement1 will not work as Log4JLogger() constructor expects argument of type org.apache.log4j.Logger type.
So, how do I use commons logging with Log4j2?
You need to add the log4j-jcl-2.7 dependency to your classpath.
See the "which jars" question in the FAQ.
In your code, use
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyClass {
private Log log = LogFactory.getLog(MyClass.class);
...
You should not explicitly use Log4JLogger.
Also, be aware that Log4j2 is different from Log4j 1 in that it offers a clean separation between its API and its implementation. So the benefits of using a wrapper library are much less now than they were 10 years ago with Log4j 1.
Consider using the Log4j2 API directly: it gives you the same separation between API and implementation and is more feature rich than commons logging or slf4j.
Note that there is little risk in using the Log4j2 API directly: the log4j-to-slf4j-2.x module is always there in case you change your mind and decide to use Logback (or another slf4j implementation) with an application that directly uses the Log4j2 API.
Related
In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects.
Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes?
Edit: Grails 3.1.1 has hibernate 5 so this is not an issue anymore...
Grails
I finally found a solution. First of all hibernate 5 implements persistent for java 8 time (https://hibernate.atlassian.net/browse/HHH-8844). However, grails 3 uses hibernate 4 and I haven't found any plans to upgrade hibernate in grails.
There is an implementation for java 8 time hibernate user types (jadira extended package) which can be used similarly as in joda time plugin (see http://gpc.github.io/joda-time/guide/persistence.html). For me only version 3.1.0.GA and lower worked.
build.gradle
dependencies {
compile "org.jadira.usertype:usertype.extended:3.1.0.GA"
}
application.groovy
grails.gorm.default.mapping = {
"user-type" type: org.jadira.usertype.dateandtime.threeten.PersistentInstantAsMillisLong, class: java.time.Instant
}
I'm developing an unmanaged extension for Neo4j.I get start with the tutorial and write a test unmanaged extension that works very well.
For some reasons,I need to add filter on requests.for example:
Execute request in transaction(like OpenSessionIView in J2EE+Hibernate environment)
Convert some chareters(like UpperCaseFilter)
...
Some people have same problem and suggest using PluginLifecycle(1,2). But my Neo4j version is 2.1.5 and this class was deprecated.
Is there any better way to add servlet filter to Neo4j server.
You can still use it, the deprecation has documentation purposes but the class will stay there until there is an alternative.
As described in Can't call one closure from another, I am using a pluggable script from within a Grails app.
Unfortunately, I've found that I can't use log4j from within these scripts. I am forced to use println.
I tried using
import org.apache.commons.logging.LogFactory
def Log log = LogFactory.getLog(getClass())
but I got no output. When I print out the result of the call to getClass(), I get something like
myscript$_run_closure5
So I'm thinking the issue is that there is no configuration in my Grails Config.groovy file for this class.
Is there a way for me to programmatically add these pluggable scripts to the log4j configuration? Keep in mind that I do not know in advance what the names of the scripts are, so this has to happen at runtime.
Consider the following code:
import org.apache.log4j.Logger
// ...
Logger log = Logger.getLogger('MyPlugin')
new File( grailsApplication.config.externalFiles ).eachFile { file ->
Binding binding = new Binding()
binding.variables.log = log
GroovyShell shell = new GroovyShell(binding)
shell.evaluate(file)
strategies.put( binding.variables.key, binding.variables )
}
Explanation:
It is not obligatory to pass class name to getLogger, it can be actually any string. You just need to make sure that this string is matched in log4j.properties of the main program.
You pass once created log to plugin scripts via binding variable "log". Then plugin scripts can access it simply as log.info('test123')
My personal recommendation would be to use logback instead of log4j. Both libraries were developed by the same guy and it is stated that logback supersedes log4j.
I'm working on a payment plugin for the grails framework. I'm using a payment provider which gives me a SOAP API (WSDL) and I need a cxf-client to communicate with the webservice.
I installed https://github.com/ctoestreich/cxf-client (cxf-client plugin) in my grails plugin project (2.2) and want to use the cxf-client I added to my config.groovy in a grails service.
In the Service Class I just added
RecurringPortType recurringPaymentClient
I don't start the plugin project directly, instead i included it in my mainproject where I use some methods of the plugins service (also autowired into my mainproject).
After using the autowired plugin service (which works) I get a nullpointer exception using a method which uses the autowired cxf-client in the plugins service class. The cxf-client bean reuccringPaymentClient is null.
But why? Do I have to include the cxf-client configuration also into my mainprojects config.groovy? Or is there a solution my mainproject can merge or also use the config.groovy of my new plugin? At this time the cxf-configuration is placed in the plugins config.groovy - Maybe that's the problem?
Using
RecurringPortType recurringPaymentClient = ApplicationHolder.application.mainContext.getBean("recurringPaymentClient")
as described in cxf-client documentation didn't help.
The Config.groovy file in a plugin only applies when the plugin is run as a standalone application itself. It is not read when the plugin is used in another application. A trick I've seen some plugins use (and which I have stolen for one of my own plugins) is to manipulate the configuration in the plugin descriptor's doWithSpring, which is usually early enough to have the required effect. In your case you'd have to make your plugin loadBefore the cxf-client plugin to ensure that your doWithSpring (creating the config) happens before that of cxf-client (which is where the settings will be used).
class MyCleverGrailsPlugin {
def version = "0.1-SNAPSHOT"
def loadBefore = ['cxfClient']
def doWithSpring = {
application.config.cxf.client.recurringPaymentClient.clientInterface = com.example.MyClientPortType
// etc. etc.
}
}
Or you can use ConfigSlurper by hand
def doWithSpring = {
ConfigObject pluginConf = new ConfigSlurper(Environment.current.name).parse(com.example.MyPluginDefaultConfig)
application.config.cxf = pluginConf.cxf.merge(application.config.cxf)
}
This loads a Config.groovy-style script from the plugin's src/groovy/com/example/MyPluginDefaultConfig.groovy, and merges the cxf section of that config into the cxf section of the main application's configuration, with settings in the app overriding the defaults supplied by the plugin.
I am trying to do salted password hashing in my Grails + Spring Security application. I have used the tutorials on the Grails site, and also ones I found randomly on the Internet.
At the moment, I have everything set up according to this tutorial. However I run into a problem when deploying the application with the following bean declaration in resources.groovy:
saltSource(cq.MySaltSource) {
userPropertyToUse = CH.config.grails.plugins.springsecurity.dao.reflectionSaltSourceProperty
}
It complains that it cannot find CH.
After digging around, I found a post on nabble stating the following:
Also - don't use ConfigurationHolder (CH) since it's deprecated in 2.0. You can pass in a reference to the grailsApplication bean and get the config from there:
saltSource(MySaltSource) {
grailsApplication = ref('grailsApplication')
}
and then in your class add
def grailsApplication
and get the property via
String userPropertyToUse grailsApplication.config.grails.plugins.springsecurity.dao.reflectionSaltSourceProperty
The part that I do not follow is the last statement about "...and get the property via...". The line of code he gives there seems malformed to me.
If anyone can shed some light here, or provide a different approach to using salted passwords with Grails and Spring Security, I would appreciate it. Note that it needs to be unique salts per user, not system-wide or a single salt, or a salt derived from username.
Thanks
UPDATE
So I got it working with the first tutorial (forgot the import statement at the top of resources.groovy. But I would still like to use the second way (to stay compatible with the future version).
UPDATE 2
I have written a complete tutorial on this if anyone browsing here is interested:
Setting up a Grails web application using Spring Security and salted passwords.
In resources.groovy where you're defining the saltSource bean the GrailsApplication is available as the application variable, so you can change the bean declaration to
saltSource(cq.MySaltSource) {
userPropertyToUse = application.config.grails.plugins.springsecurity.dao.reflectionSaltSourceProperty
}