Google Cloud Platform Log Viewer logs truncate jsonPayload message for error log - spring-boot

The logs I'm viewing in the Log Viewer are truncating the "message" part of the payload, but the "exception" part of the payload doesn't have a limit and shows the entire stacktrace. These logs are for exceptions caught in my service running in Google Cloud.
The service is built with Spring Boot using SLF4J as a logger factory. I use SLF4J's LoggerFactory to create a logger based on the class where it's invoked and when an exception is caught, I log using the logger's error("Exception thrown processing this message: ${message.data}", exception) where message is of type com.google.pubsub.v1.PubsubMessage and exception is a Throwable of type java.lang.Exception.

I had the same problem.
The only way is to jump to Logs Explore and watch the log.

Related

Configure Spring Security to only log the Exception without Stacktrace?

I like to have Spring Security log what's going so
logging.level.org.springframework.security=debug
is enabled.
However, when a user uses bad credentials or is not activated, each time an exception with about fifty lines of (uselesss) Stacktrace is logged.
I'd like to get only the exception
org.springframework.security.authentication.DisabledException: User is disabled
without stacktrace.If the logging level is set to info, there however is no log if there was an logging attempt at all.
Is that possible? - without disabling exceptions at all

Spring boot common logging

I'm very new to the spring boot..
I understood that Spring Boot uses Commons Logging for all internal logging.
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error
after adding this into application.properties file, application logs all the output and exceptions in the console..
so I have a requirement to print the logs only when the system throws the error not for the success response..
so I have a requirement to print the logs only when the system throws the error not for the success response..
The implementation of this requirement IMO heavily depends on your system
From the word "response" I understand that you're working with some kind of controller (like in spring mvc). But the controller method is only an entry point to your backend.
What if the controller calls the service that logs something (message logA), then (after logging) it calls another service that again logs something (message logB) and then in turn calls, dao to call the datatbase?
If, say, DB threw an error, the logA and logB messages are already logged and you can't "take that back".
So, in general, you can log that there was an error by explicitly catching the exception in controller and logging the error, or using Controller advice to intercept and log the exceptions "globally".
When you reach the point where you log the message you can log it with severity, say, ERROR and the logging framework will log it as long as its configured to log messages of that level from that logger.

Logging after exception on application start

Using Spring boot 2.0.5. On application start occurs a exception with is logged 'ConfigServletWebServerApplicationContext : Exception encountered during context initialization'. But i need stacktrace. I was expecting stack trace will be logged later in SpringApplication.reportFailure '"Application run failed"'. But spring boot calls before this method ServletWebServerApplicationContext.stopAndReleaseWebServer and its seems that after this call any logging does not work anymore.
this can be related to your issue: spring boot discussion
spring boot github issue
You can also debug SpringApplication#reportFailure in the end (after catch (throwable)) it has generic logging of any error:
logger.error("Application run failed", ex)
In my case I had the logger from commons-logging which prevented logging to happen.

Zipkin With Spring Boot

I am using spring boot and zipkin.
And using spring slueth to generate trace Id.
Is there a way I can generate this trace Id on my own?
Also I want to log only specific requests say with 500 error or response time > threshold, how to do this?
To log only request with particular error you can add the log in your exception mapper where you are handling those error.
To show the log for error response you can set like below,
#Autowired
private Tracer tracer;
and set
tracer.addTag("error","Your message")

Custom Logger for Elmah producing error when trying to read the logs

We are trying to implement a custom logger for Elmah to log errors in a RavenDB database in an MVC5 application.
The Logging works perfectly but when we try to read the logs we get an exception for all properties of the Error Class that are of type NameValueCollection with the following error :
Cannot populate list type
System.Collections.Specialized.NameValueCollection. Path
'Error.ServerVariables'.
Any assistance would be greatly appreciated

Resources