How to disable and enable jetty request log dynamically? - java

How to disable and enable jetty request log dynamically in a web project?I want to disable the request log when the system resource is pretty low which may save the world.

Use the Slf4jRequestLog implementation for Request Logging and simply enable/disable the selected Slf4j implementation using the techniques that the implementation provides.
You cannot rely on the behaviors of the default implementation in your use case.

Related

Is it possible to create custom fields in a Kibana dashboard?

I am using a Java micro-service architecture in my application and generating separate log files for each micro-service.
I am using ELK stack approach to visualize the logs in Kibana, but the problem is whatever the fields that I'm getting from Elastic Search that are related to server logs fields. some example fields are #timestamp,#version,#path,#version.keyword,#host.
i want to customize this fields by adding some fields like customerId,txn-Id,mobile no so that we can analyze the data easily.
I'm using org.apache.logging.log4j2 to write the logs. Can I set above fields (customerId,txn-Id,mobile) to log files? And then Elastic will store these fields with the above default fields and then these custom fields should available in a Kibana dashboard. Is this possible?
It's definitely possible to do that. I've not done it with the log4j2 stack (I have with slf4j/logback), but the basic approach is:
set those fields in the Mapped Diagnostic Context (I'm fairly sure log4j2 supports that)
use a log appender which logs to logstash-structured JSON
configure filebeat to ship the JSON logs
if filebeat is shipping to logstash, you'll need to configure logstash to pass those preformatted JSON logs directly to elasticsearch
It is definitely possible. I am doing that now with my applications. However, the output looks a bit different from yours. The basic guide for doing this can be found at Logging in the Cloud on the Log4j2 web site.
The "normal" log view looks very similar to what you would see when logging to a file.
However, if you select a message you can see the individual fieds.
The Log4j2 configuration uses a TCP Socket appender that is configured to write to a cluster of Logstash servers that use a single DNS entry and to use the Gelf layout.
You can also use MapMessages to capture individual data elements and log them. While this currently works it is slightly cumbersome so I have recently committed improvements that will be available in Log4j 2.15.0.
It is important to note that the Logging in the Cloud page briefly mentions storing your logging configuration in Spring Cloud Config. If you want to have a common base configuration while allowing apps to do some customization this works very, very well. However, The Gelf, Json Template Layout and TCP Appender are all independent from that and can be used without Spring Boot.

How to check logging implementation details in Spring Boot

I am using Spring boot.
I want to check which logging implementation is printing the message - I know with Spring boot default is Logback, and I have excluded it as mentioned in this post so mostly Logback will not be printing the messages, but I want to show it as a proof that Logback implementation is not printing and probably Log4j is printing.
Basically I need an API which I can call and I can get the details of which is the logging implementation, the way we can know Java version etc.
You can enforce Spring Boot to use a certain implementation by setting this property:
org.springframework.boot.logging.LoggingSystem
with any of:
org.springframework.boot.logging.logback.LogbackLoggingSystem
org.springframework.boot.logging.log4j2.Log4J2LoggingSystem
org.springframework.boot.logging.java.JavaLoggingSystem
none (to swith off completely)
This explicit configuration would be your proof.
To check configuration you can install spring actuator framework. Through web endpoints all config params can be queried.

Dynamic | User case Based logging

Is there any logging framework, which helps me change logging levels dynamically based on the request parameters received ?
If request has a parameter with debug enabled to true, then only it should log, else not.
Does spring sleuth provide this feature in cloud environment?
You can use Spring Boot & Spring Cloud Config and standard Slf4j logging mechanism. You can check out this answer for more information - Managing logging.level using ConfigServer
If you just want conditional logging you would use a NDC/MDC and a filter using the frameworks that support that feature.
If you want something more general then for instance, setup a com.foo.request that is set to say INFO and a com.foo.request.debug that is set to some lower level. Pick and choose the logger on request parameter.

Using custom logging filter with LoggingFeature for Jersey Client.

I am using Jersey 2.22. I have registered a LoggingFeature that logs requests, responses, headers, payload, etc based on the verbosity that I set however I would like to implement my own custom logging filter that this feature will use.
Looking at the source of LoggingFeature, it doesn't appear this is possible. This seems very inflexible. With previous versions of Jersey, it was very simple to implement a LoggingFilter and register it on the client.
I'd really like to implement my own version of ClientLoggingFilter so I can write a neat line to my log.
Any suggestions? The documentation is lacking IMO. https://jersey.github.io/documentation/latest/logging_chapter.html#d0e15744
The solution for me was to implement my own version of a LoggingFeature with a custom request/response container injected. Within the container I did the exact logging that we required.
Seems like a step backwards not being able to implement a simple filter and apply it to the client for things like custom logging.

How to use JBoss logging brought by Hibernate?

I am writing standalone java application which is using Hibernate. Maven brought jboss-logging library for me. I am not using JBoss. The question is: can I log with this library only, or I need to download some logging implementation like log4j?
JBoss Logging is just a logging facade. To configure your loggers, e.g. use/add handlers, you need a log manager like JBoss Log Manager, the J.U.L. log manager, logback or log4j.
JBoss Logging will attempt to discover which log manager is being used. You can specify which log manager you'd like to use with the org.jboss.logging.provider system property. The allowed values for `org.jboss.logging.provider' are:
jboss - for JBoss Log Manager
jdk - For the J.U.L. log manager
log4j - For the log4j log manager
slf4j - For logback with slf4j
Hibernate uses JBoss Logging for it's i18n capabilities, it's vararg logging methods and the ability to not be tied to a log manager.
Of course you can absolutely use JBoss Logging in your project. If you want to configure logging handlers you'd also have to use a log manager as well.
afaik, jboss-logging is more a extra layer on top of normal logging api, to provide more sophisticated feature like i18n etc.
JBoss-logging can use other logging library (e.g. SLF4J) as the underlying handler for log.
I believe if you are writing a simple standalone Java app, you do not need to use JBoss-logging (unless you know you really want and need to do so).
Using SLF4J (with LogBack or Log4J binding) will be a good choice. Visit http://slf4j.org for more information
Make sure you have jboss-logging and your logger implementation in your classpath and set the system property org.jboss.logging.provider to log4j, jdk, slf4j or jboss depending on what you want. In theory autodetection may also work.
https://github.com/jboss-logging/jboss-logging/blob/master/src/main/java/org/jboss/logging/LoggerProviders.java#L29

Categories

Resources