Monday, May 26, 2014

To get http status code from curl call


Buy Moto series phones: Moto E, Moto G, Moto X or Other phones
Buy eBooks from Flipkart
-----------------------------------------------

We can use below command to get http response code of a URL from bash script.

status_code=$(curl --write-out "\n%{http_code}\n" --silent --output /dev/null --insecure https://www.google.co.in/)

silent - to stop progress information in output
output - we are re-directing normal output to null device
write-out - whatever things we needed can be given here. Here we are printing http status code. It will be printed after completion, i.e., after printing all output.
insecure - this allows connections to SSL sites without certs

References: http://stackoverflow.com/a/2220646/324900

Wednesday, May 7, 2014

Overriding logback file embedded in jar file


I was trying to run a jar file by setting it up in cronjob. Now this jar file contains a logback configuration file included in it which is causing me problem because all logs are being written to a specific file name, like processnamelogs.log. All runs of jar file will append to the same log file.

Now this is not what I wanted since the log file is going to grow huge in few runs. Also if all runs of jar file are writing to same log file it will be difficult to setup a monitoring script to check for errors.

What I wanted is that each run of jar file writes to a separate log file with its name containing start time of that run. So it will be easy to go through logs in case of errors. Also I can easily setup a monitoring script to only search in the log files with current date in their names.

So I need to write my own logback configuration file and a way to make sure the jar file uses my configuration file instead of embedded one. I have searched and got one system property (logback.configurationFile) which does what I wanted.

e.g.,
  java  -Xms128m -Xmx512m -XX:MaxPermSize=256m -jar -Dlogback.configurationFile=./mylogback.xml ./process.jar

mylogback.xml


<configuration>

  <!-- Insert the current time formatted as "yyyyMMdd'T'HHmmss" under
       the key "bySecond" into the logger context. This value will be
       available to all subsequent configuration elements. -->
  <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <!-- use the previously created timestamp to create a uniquely
         named log file -->
    <file>log-${bySecond}.txt</file>
    <encoder>
      <pattern>%logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>






References:
  • http://logback.qos.ch/manual/configuration.html#configFileProperty
  • http://logback.qos.ch/manual/appenders.html#uniquelyNamed












Latest Posts