Is it possible to create user defined level of LOG in java? - java

Is it possible to create a user-defined level of log in java? If yes can someone give me an example?
I have an web application, and some web services exposed by the same. I want to create a new log file for every new request. Is there any way I can do it? If yes, please share the solution. Thank you in Advance :)

You can use something like MDC(Mapped Diagnostic Context) in LOGBack.
For WEB application javax.servlet.Filter can be used to set context values for every request.
example: http://logback.qos.ch/xref/chapters/mdc/UserServletFilter.html

Related

Logging request-response from java service

I have generated some proxy classes that are consumed by a Java Service. (using WSDL2JAVA)
I need to find a way to log requests and responses whenever the proxy class invokes the respective external web service.
Is there any easy way to achieve this?
Thanks!
Silvio.
If you are using Axis, configure log4j like this:
log4j.rootLogger=ERROR
log4j.appender.axisLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.axisLogFile.File=soap-messages.log
log4j.appender.axisLogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.axisLogFile.layout.ConversionPattern=[%d{DATE} - %-5p] %m%n
log4j.logger.org.apache.axis.transport.http.HTTPSender=DEBUG, axisLogFile
This is awesome, you need add the following for in java code.
PropertyConfigurator.configure(<log4j property file>);

How to retrieve weblogic domain home?

hi guys I want to retrieve the name and path of the domain of weblogic from my start up class. how can i achieve this?
Take a look at the getCurrentDirectory() of ServerRuntimeMBean
I haven't confirmed this, but you may be able to get at this information using an Application Life Cycle Listener.
An example here.
You can get a AppDeploymentMBea from the ApplicationContext in the ApplicationLifecycleEvent. AppDeploymentMBea has an InstallDir.
Java EE:
ServletContext with a Listener can use getRealPath("..."), provided you let your deployment be not as war (where there are no files), but for instance as unpacked war. Otherwise getRealPath would yield null. Try getRealPath.

How to create JDBC datasource and access database file dynamically or programmatically

I m writing one application and want to make it opensource. My app uses access database, and i created manually the datasource using run->odbcad32. When I post that app in site , those who download need to run it without much efforts, and they should not need to create access file and data source.,
1.how can i create the ms access file programatically?
2.how can i create the datasource programatically?
any other ideas to do the same?
Write a batch (*.bat) that start odbcad32.exe and your application.
This sort of thing can be set outside of the application in a config file, not so different from how Spring does it. You can even give them a small gui to set this sort of thing in your application as well, either in the options or even when the application starts up for the first time.
It may make more sense to allow users of your application to pass in the data source.

How do you differentiate log4j sessions in a log file from copies of the same web-app?

There is only one file. And it is written simultaneously as web app copies run.
How do you filter only one session log messages from other log lines?
Using a servlet filter with either NDC or MDC information is the best way I've seen. A quick comparison of the two is available at http://wiki.apache.org/logging-log4j/NDCvsMDC.
I've found MDC has worked better for me in the past. Remember that you'll need to update your log4j properties file to include whichever version you prefer (pattern definitions at http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html).
A full example of configuring MDC with a servlet filter is available at http://veerasundar.com/blog/2009/11/log4j-mdc-mapped-diagnostic-context-example-code/.
A slightly easier to configure, but significantly inferior option: You could opt to just print out the thread ID (via the properties file) for each request and make sure that the first thing you log about each request is a session identifier. It isn't as proper (or useful), but it can work for low-volume applications.
You could set a context message including the identifier of the specific app instance using org.apache.log4j.NDC, like this:
String appInstanceId = "My App Instance 1";
org.apache.log4j.NDC.push(appInstanceId);
// handle request
org.apache.log4j.NDC.clear();
You can set up the context during the initialization of your web app instance, or inside the doPost() method of your servlets. As its name implies, you can also nest contexts within contexts with multiple push calls at different levels.
See the section "Nested Diagnostic Contexts" in the Log4J manual.
Here is a page that sets up an MDC filter for web-app -> http://rtner.de/software/MDCUserServletFilter.html
Being a servlet filter it will free you from managing MDC/NDC in each of your servlets.
Of course, you should modify it to save information more pertinent to your web-app.
If you want to differentiate sessions in the same application then the MDC is the way to go. But if you want to differentiate the web applications writing to the same file, then MDC won't help because it works on a thread basis. In such case I used to make my own appender which knows which application instance it serves. This can be done through appender configuration properties. Such appender would stick application name into each logging event as a property before writing it into the media, and then you can use a layout to show this property value in the text file it writes to. Using MDC in such case won't work because every thread will have to MDC.put(applicationName) and that is quite ugly. MDC is only good for single process, not for several processes. If someone knows the other way, I'd like to hear.

Exchanging of screens between two users

We have a web-based application with tech stack -
1. Java Struts based
2. Hibernate
3. DB - Oracle
4. App server - JBoss server
We are facing an issue related to concurrent usage of the application by two or more users. When I am doing an operation and I submit the changes, the next page or success message that comes up is of a different operation that another user is performing at the same time.
Users are logged in as different users and so are using different sessions.
We have no clue of where the problem is, so I am not sure what other details I can provide.
Has anyone else faced such an issue or any pointers?
Are you using application context instead of session context? Moreover, as Eed3si9n said, beware of Singletons, that might be causing this.
"In addition check for the use of static fields. One app I was brought in to fix used a static string for error message. As soon as any user received an error they all did. Worked fine until there wasmore than one concurrent user." – Michael Rutherfurd (posted it as a comment)
I am not familiar with specific libraries you are using, but let me try.
How stateless are your application code? Do you have any sort of global state like singleton with member fields? If the service is stateful and are using singleton, you could have such mixups.
Check if the form is defined as application scope and the message you showing on the screen is coming from that form.

Categories