Some logging considerations for our disastrous code

Experience indicates that logging is an important component of the development cycle. It provides precise context about a run of our shitty application. 

But that precision with logging, only appears when used conscientiously xD

We usually use logging for:

  • aid in problem diagnosis in development without requiring a debug session.
  • aid in problem diagnosis in production where no debugging is possible.
You can even use logging to: 
  • help educate new/old developers in learning/remembering the application.

All that sounds really nice for sure! right ? 

The truth is, that, the crude reality reveals something even worse than our disastrous code.

And if you use threads, oh my!!! 

The problem appears when we abuse of logging and when we use it with no path.

That path must include the use of the correct type of logging with the correct info. 

And we need a good path not only for us, but also for those who will help us.

Having a terrible path is as useful as no using any kind of logging.

Because, a terrible path does not help to follow the flow through the system. 

 Now lets try to fix this mess xDDD.

Thats all folks.


ALL > TRACE > DEBUG > INFO > WARN > ERROR > FATAL > OFF or how works Log4j levels in our shitty application

Dealing with Log4j will push us inevitably (but in a good way) to work with Appenders and Levels.

When that moment arrives, and we configure our log4j.properties to just INFO messages.

We will discover there are other type of messages.
And thats because of Levels! There is a hierarchy!

Works in this way:

TRACE:   Shows messages classified as DEBUG, INFO, WARNING,ERROR and FATAL
DEBUG:   Shows messages classified as DEBUG, INFO, WARNING, ERROR, and FATAL
INFO:    Shows messages classified as INFO, WARNING, ERROR, and FATAL
WARNING: Shows messages classified as WARNING, ERROR, and FATAL 
ERROR:   Shows messages classified as ERROR and FATAL
FATAL:   shows messages at a FATAL level only
:     You are a genius! Turn on all logging.

OFF:     Yes you are! Turn off all logging.

Let's put this into a drawing:

And that's all folks.


logstash and Apache log4j or how to manage all of your events and logs from all over your infrastructure in one place.

Having deployed our applications all over the world on clients server, usually force us to monitor and support those applications.
Few years ago, the normal operation was: log into server clients and discover what is happening based on our application logs.

Yes, watching the logs. The system or application health was controlled with some monitoring system.

But the application logs, always remained at clients servers.

This is a problem, because of time consuming just trying to get remote access to servers.

Why a problem? Because the IT departments of those clients usually sucks.

So the idea now is to externalize all those logs to a central server.

In this way we will be able to detect problems faster and prepare fixes quickly.

This is nothing new, there are a lot of solutions available for this.

In this case we are going to play with "logstash" and "log4j" for sending out logs to the "central" server.

Some description before continue:

logstash is a tool for managing events and logs. You can use it to collect logs, parse them, and store them for later use (like, for searching). Speaking of searching, logstash comes with a web interface for searching and drilling into all of your logs. For more info http://logstash.net/

Apache log4j is a logging library for Java. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary. For more info http://logging.apache.org/log4j 

Now, with that background, lets start.

First, we need to configure logstash to accept all logs coming from our applications or servers (everything using log4j can talk to logstash)

logstash.conf content:

input {
 log4j {
     mode => "server"
     host  => ""
    port => 56445
    debug => true
    type => "log4j"

output {                                       
  stdout { debug => true }                     
  elasticsearch { embedded => true }           

We are telling to logstash that there will be and input of type log4j that will listen on port 56445, in this way, the info will be correctly handled. Also, we are telling to start it with elasticseach embedded (a powerful open source search and analytics engine that makes data easy to explore)

We that wonderful configuration is time to run logstath:

java -jar logstash-1.2.2.dev-flatjar.jar agent -f logstash.conf -- web 

We are running logstash with the agent mode and web interface using the config file logstash.conf
The web interface is available at port 9292.

Time for the java app.

First, the log4j config file, usually log4j.properties

# output messages to stdout and over the network

# stdout
log4j.appender.stdout.layout.ConversionPattern=%5p %d{HH:mm:ss,SSS} %m%n

# logging over the network. Set port and IP for your network

Now a very simple sample:

package jtest;

import org.apache.log4j.Logger;

import java.io.IOException;

public class JTest {

    static Logger log = Logger.getLogger(JTest.class.getName());

    public static void main(String[] args) throws IOException, InterruptedException {

        log.debug(String.format("Hello this is an debug message %1$,.2f", Math.random() ));
        log.info(String.format("Hello this is an info message %1$,.2f", Math.random() ));
        log.error(String.format("Hello this is an ERORRRRRRRRRRRRR ERRORRRRR ERRORRRRRR %1$,.2f", Math.random() ));


And thats all folks, just hit F5 on the browser (because you are at http://localhost:9292 rigth? ) and that info should appear in logstash.


Expressive power or why programming in java feels like shit.

Lets take Java and Python

You can find a lot of benchmarks that shows that Java is faster than Python, a lot faster. That's a fact!


Why didn't it feel that way when coding in Python versus Java?

Why you can feel that doing a task in Python is a way a lot faster and "cheaper" than doing the same in Java ?

And i'm not going to publish the "hello world" comparison xD


Why when you try Python, you start loving the simplicity, the flexibility, the terseness, the rapid development?

Why Java doesn't offers those feelings?

Don't get me wrong, I'm primarily an Android/Back-end Developer and i have to use Java at-least 40 hours/week.

And I still hate it. It limits creativity by bogging down me in an ocean of shit.

That's why, i started to think about programming languages "productivity".

How a programming language is or feels more productive or not?

Why this language is "better" or not?

 With those feelings and some search, i just found the answer: Expressive Power

"In computer science, the expressive power (also called expressiveness or expressivity) of a language is the breadth of ideas that can be represented and communicated in that language.

The more expressive a language is, the greater the variety and quantity of ideas it can be used to represent."

Source: http://en.wikipedia.org/wiki/Expressive_power

Knowing this, and making use of my bad memory, i just remembered one of those fast read pages on Code Complete 2Ed (I'm guilty for fast reading):

"Table 4-1 shows typical ratios of source statements in several high-level
languages to the equivalent code in C. A higher ratio means that each line of code in the
language listed accomplishes more than does each line of code in C."

So, this, shows the Expressive Power of these languages.

In some way this also shows how high-level a language is ? Just thinking on those studies that shows that programmers working with high-level languages achieve better productivity and quality than those working with lower-level languages.

Of course, the expertise level of individual counts when performing a task.

For a better reference, check the papers at the bottom of the Table 4-1.

I know this data is "old", i would like some recent studies,

but coding in java still feels like shit and now i really know it can be measured (just saw Prechelt 2000)

Creative Commons License This page is powered by Blogger. Isn't yours?