I'm creating a Java JWS application. That application is logging some useful (for me) debug stuff to the system.out. Now, application will be used by 3rd party and i don't want them to see debug log.
My idea is following: application should write all the status messages to the custom stream. If logged user is "dev", then custom stream should be "merged" with system.out and console should print all new and prior (already existing) data from my custom stream. If logged user is somebody other than "dev", all status messages will remained logged into the custom stream and won't be visible in the console.
How could i achieve this functionality?
You can use Apache's log4j or commons logging with cutom filters. There's a Logger that comes with the JDK. You can also create a custom logger considering the types of users. You can direct the logs in proper output destination depending on the user type. All logs should be stored somewhere regardless of user type to monitor though.
Related
I have a Java application running on Google App Engine standard environment.
I am able to log from it (using JUL). In standard environment, all the application log lines from a single web request are grouped into a single entry in the request_log. Everything runs great
However, now I have a requirement to add custom labels to a log entry for a request. For example, what is user ID associated with it.
Stackdriver documentation (https://cloud.google.com/logging/docs/setup/java) gives example how to "enhance" log entries with custom labels. However, it appears that the page does not apply to standard environment.
Is it possible to add labels (or any information associated with the log entry other than app log lines) to a log entry in request_log and how? If not, what are the alternatives?
The log enhancer would allow you to add a custom labels although it would be hard coded as this function (enhanceLogEntry(LogEntry.Builder logEntry)) is called at the end of the request when the log is being populated. Supplying a value from the request to appear in the request_log made by the application would not be possible.
I do not see how it would be limited to the flexible environment, you should be able to do it from the standard environment as far as I can tell.
Alternatively, I believe the best path would be to write your own logs entries by using the Stackdriver Logging Client Libraries within your request code.
I have 4 appllications (ear) on my WAS. I need them to write in SystemOut.log some sign. I.e each application must write to log file its own sign. For example:
[16.01.17 3:50:05:592 GMT+05:00] ADMIN 000005e0 SubsystemMess I com.docflow.core.integration.jms.SubsystemMessageListener onMessage_aroundBody0 Subsystem integration message ID:f5392a5ec3b3f41502095b00110a134f0000000000000001 of type DP_EKS_BANK_GUARANTEE_RECEIVED process finished
Here ADMIN is sign of the application.
How i can do that?
It's not possible to adjust the default logging format in this way. Your options would be to use java.util.logging.Logger with a particular name (to replace the "SubsystemMess" part) or to use a separate logging package and configure it to log to a separate file.
Look at log4j and configuring each app's log4j to go to a different file.
Log4j's file appender can be configured to also include timestamp and more.
The only catch is you can't configure it from WAS's admin console then.
Note that java.util.Logger writes to the trace.log. If you use a different logger name, it will list as such in the same trace.log file. Upside is you can now configure it from the admin console.
One potential solution could be to switch to HPEL logging. Then you could query log entries for your given application using logviewer, like:
logViewer.sh -includeExtensions appName=PlantsByWebSphere
I know that it is not exactly what you are looking for, but maybe it will be sufficient for your needs.
I would like to inject a piece of information into all messages logged by anything, including third-party code. The reason is that, in our web-services-oriented application, requests come in with unique IDs and I want those unique IDs to be attached to all log messages that occur while processing a request, to assist in later analysis. I am already tracking the "current request" using ThreadLocal<> techniques, so I have the ability to fetch the "current request" from anywhere.
To that end, I would like to configure log4j such that I can inject the requestID into messages before they reach the root logger (or appender?). I know that I can just make a whole new Appender that implements append() and does whatever it wants with the output, but that's not what I'm asking for. I want output to ultimately go to whatever appender is configured at startup, but with the additional information attached.
I am using log4j 1.x, but if a move to log4j 2.x or using slf4j makes this significantly easier, I would consider it.
I have a Swing-based application that logs all messages to text files through slf4j with logback underneath.
I'd like to add a feature to show all messages at a certain level(e.g. fatal) logged in the current session on demand, say in a JTable.
Does slf4j provide API that lets you access historical log messages, preferably filtered by level or time?
Try to use Logback, there is a ch.qos.logback.classic.db.DBAppender class that you can use as an Appender to your fatal errors. You can define your own data structure, just provide the SQL Insert statement. Also, other variants of this DBAppender are provided, so you can choose when do you want to customize the behavior.
The next thing is that you tie your appender to those loggers that you want to log.
Finally you can manage your logged data within your application (filter, purge/archive) just like your application business data.
I have Log4J with SLF4J working in my app, and right now it logs the request in a standard format and it also sends default information to the syslog. I do both because there is some information that I want with each request logged - account and/or user id making the request, the request method type - post, get, etc.
Is there a way to send the same custom information to the syslog?
From what I have found you can alter the format of the log entry, but you can't append additional information to the syslog as that would screw up the format and may not be pulled into various reporting tools and you can't change the contents of a piece of the log - for example, changing the category to hold the request method type. I was thinking that route so the tools would pick up the data.
Is there a good tool that you can specify the log format and it can parse it, or it handles different log formats? I need to know more about who is doing what where so I need to create custom reports, rather than the standard - how many hits per page, etc.