How to see HTTP request details that Apache HttpClient sent out [duplicate] - groovy

For debugging purposes, I'd like to see the raw request that is going to be sent. Is there a way to get this without a HTTP monitor directly from the API of HttpPost or HttpClient?
I found some "almost" duplicate questions, but not for this particular one

You can set some environment variables for Apache HttpClient (example tested for 4.3.2).
System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG");
There are also some more variables for debugging:
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.impl.conn", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.impl.client", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.client", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG");

org.apache.http.client.fluent.Request#viaProxy
This method can make your request pass through proxy server, so your can launch a local proxy server, for example Fiddler, so this debugging proxy can show the details of http request and response.

Try enabling DEBUG mode in your logging configurations, if you're using log4j you can add this to the log4j.xml file of your project
<root>
<priority value="DEBUG" />
<appender-ref ref="FILE" />
</root>
You will have the full request headers, params, body, etc, logged in your logs files.

log4j2 and/or slf4j solution
For everybody using log4j2 and/or slf4j all the mentioned solutions didn't work.
I added the following to my maven pom:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.30</version>
</dependency>
Add a log4j2.xml file / or use your existing one and add:
<Logger name="org.apache.http">
<Level>DEBUG</Level>
</Logger>
Of course for a valid log4j2.xml configuration you need some appenders defined etc. A simple example can be found here

Try this:
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
method.setParameter(...., ....);
to retrieve the URI
System.out.println("getUri: " + method.getURI());
to retrieve the parameters in POST
method.getRequestEntity().writeRequest(System.out);

Related

Logging with applicationinsights in spring boot app

We are using spring boot to send metrics to app insight we are using applicationinsights-logging-log4j2.
Below are the appenders we are using in logj2-spring.xml
*
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{MM-dd-yyyy'T'HH:mm:ss.SSS,UTC} %correlationId [%thread] %-5level %logger{36}- %msg%n"/>
</Console>
<ApplicationInsightsAppender name="aiAppender">
</ApplicationInsightsAppender>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
<AppenderRef ref="aiAppender" />
</Root>
</Loggers>
We are seeing the logs in app insight search screen however i have few questions.
Is there a way to define a custom info in logging like correlationId(guid used to track a flow uniquely) and send it to AI just like we are appending in console logs.
Is there anything like pattern we can define for AI.
Is there a use of console appender and logging to console if we are logging to AI.
You can create a class that will extend the OncePerRequestFilter, and in that class generate one Id using UUID generator and set this UUID in variable, let's say RequestId.
And then write MDC.put('requestid', RequestId).
OncePerRequestFilter class is executed with every HTTP request, you won't be required to call the class extending it explicitly, and MDC.put('requestid', RequestId) will be added as external property in your application insight log.
This is just a workaround for correlationid though it is providing us a same feature, that we can aggregate a log. Whatever requestid is being generated, you can retrieve that and then use it application insight to see logs for that request.
I believe console appender is still helpful, because I. AI we can see loga after at least 4 to 5 mins, so for real time debugging console logs are helpful. Though you can. Configure what type of logs you want to see in console and what you wanna sent to ai.

How to log HTTP requests sent by ODataQueryBuilder API / VDM API?

Using latest version of Java SAP Cloud SDK
We have some code which uses ODataQueryBuilder API and VDM API as well. We want to log the HTTP requests that are being sent by these API's. We want to log whole of the HTTP request - headers, body everything. Please note that our application is running on SAP Cloud Platform's Cloud Foundry PAAS offering and using cf set-logging-level doesn't seem to work.
I've been using this Java arg when debugging my requests, but I've been doing it locally.
-Dorg.slf4j.simpleLogger.log.org.apache.http.wire=debug
If you can pass it withing CF environment I think you should start seeing all the payloads. I'll research a bit more to provide a better guidance if this won't work for you.
For applications deployed on SCP CF, there are different setups for which recommend other logging practices. The goal is to configure individual log levels for specific packages of your application and third-party dependencies, e.g. SAP Cloud SDK or SAP Service SDK or Apache HTTP components.
TomEE based application:
Edit the manifest.yml to include the following env entry for environment variable:
SET_LOGGING_LEVEL: '{ROOT: INFO, com.sap.cloud.sdk: INFO, org.apache.http.wire: DEBUG}'
Feel free to customize.
Spring Boot based application:
We expect the logback framework.
Edit/Create the file: application/src/main/resources/logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProfile name="!cloud">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<root level="INFO"/>
<logger name="org.springframework.web" level="INFO"/>
</springProfile>
<springProfile name="cloud">
<appender name="STDOUT-JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="com.sap.hcp.cf.logback.encoder.JsonEncoder"/>
</appender>
<logger name="org.springframework.web" level="INFO"/>
<logger name="com.sap.cloud.sdk" level="INFO"/>
<logger name="org.apache.http.wire" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="STDOUT-JSON"/>
</root>
</springProfile>
</configuration>
Feel free to customize.
Notice the different profile settings. Make sure the cloud profile is active for deployed applications. Edit the manifest.yml to include the following env entry for environment variable:
SPRING_PROFILES_ACTIVE: 'cloud'

Printing to console not working in Karate

I have scenario which tests an Micro service.
Scenario Outline: Run Object Queries
Scenario Outline: Run Object Queries
Given url homeLinks.queryUrl
And header Content-Type = 'application/x-www-form-urlencoded'
When method post
Then status 200
Then print response
Print statement is not logging anything to console in InteliJ editor. I tried to run in debug mode as well.
I referred KARATE : can not see print statements on console in karate which didn't help.
Any config changes required to print the logs?
Normally people don't run into issues. Refer the docs here: https://github.com/intuit/karate#logging
If you have mixed Karate into a project with a lot of other dependencies this may happen. I suggest you try the Maven quickstart and that will work correctly out of the box: https://github.com/intuit/karate#quickstart
Then it is a matter of comparing the setup with your project. If you still get stuck, follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
In logback-test.xml under test/resources, When I change the Console level from WARN to INFO, I can see the logs. My application is spring boot application
<root level="INFO" additivity="false">
<appender-ref ref="Console" />
</root>

log4j with bluemix cloudant DB

I am trying to store log4j2 logs in bluemix's cloudant DB.
Could you help me out or point to any document , regarding log4j2 configuration I need to make ?
Thank you.
Take a look at the Log4j 2 Docs - Appenders. The NoSQLAppender writes log events to a NoSQL database using an internal lightweight provider interface. Provider implementations currently exist for MongoDB and Apache CouchDB, and you can write a custom provider.
You specify which NoSQL provider to use by specifying the appropriate configuration element within the <NoSql> element. The types currently supported are <MongoDb> and <CouchDb>. To create your own custom provider, read the JavaDoc for the NoSQLProvider, NoSQLConnection, and NoSQLObject classes and the documentation about creating Log4j plugins.
Considering that Cloudant is built upon CouchDB you should be able to adapt the CouchDB appender for your purpose. The following is an example of appender configuration for CouchDB:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<NoSql name="databaseAppender">
<CouchDb databaseName="applicationDb" protocol="https" server="couch.example.org" username="loggingUser" password="abc123" />
</NoSql>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="databaseAppender"/>
</Root>
</Loggers>
</Configuration>

JBoss 6.0 M3 and Log4j logging

I'm trying to have application specific logging plus the usual server logs seperately. I have specified 2 in the jboss-logging.xml, one for my app and the other one is usual "server.log" file.
Issue is, both of the log files are getting created and logged at the same time. Can any one help me, what is the change i need to make to log ONLY application specific logs in my logfile?
I fixed this issue. I was defining my application's handler on the section in jboss-logging.xml.Plus i was defining a logger category for my app. The fix was, i removed my application's handler reference from <root-handler> section and hold just the logger category for my app as below:
<logger category="com.X.Y.logger">
<level name="DEBUG"/>
<handlers>
<handler-ref name="myApp"/>
</handlers>
</logger>

Resources