When you run a transformation or a job, all of what is happening in the process is shown in the Execution Results window, which has a tab named Logging where you can check the execution of your transformation step by step. By default, the level of the logging detail is Basic, but you can change it to show different levels of detail.
Under the Logging tab, you can see information about how the step is performing, for example, the number of rows coming from previous steps, the number of rows read, the number of rows written, errors in execution, and so on. All this data is provided by the steps automatically, but what if you want to write your custom messages to the Logging information? To do this, there is a step and an entry named Write to log, in the Utility folder.
To put them into practice, let's take a simple transformation that reads a text file with book novelties and splits them into two Excel files depending on their price. The objective here is to include, in the Logging window, custom messages about the incoming number of books and also how many of these books are cheap or expensive.
For checking this recipe, you will need a text file that includes information about book titles novelties. For example:
title;author_id;price;title_id;genre Bag of Bones;A00002;51,99;123-353;Fiction Basket Case;A00003;31,00;123-506;Fiction Carrie;A00002;41,00;123-346;Fiction Cashflow Quadrant;A00007;55,00;323-604;Business Harry Potter and the Half-Blood Prince;A00008;61,00;423-005;Childrens Harry Potter and the Prisoner of Azkaban;A00008;29,00;423-003;Childrens Power to the People;A00005;33,00;223-302;Non-fiction Who Took My Money;A00007;21,00;323-603;Business
You can download the sample file from the book's website.
Carry out the following steps:
; as the character Separator under the Content tab. Finally, use the Get Fields button under the Fields tab to populate the grid automatically.Previewing this step, you will obtain the data of the books from the text file. Now, let's add the steps for counting the books and writing the information.
qty
, choose a field (for example, title_id)
in the Subject column, and select the Number of Values (N) option in the Type column. line
of String
type with the following Java expression:"Book news = " + Java.lang.Long.toString(qty)
Write books counting
. Add the line
field to the Fields grid. Choose Basic logging
in the Log level listbox.Run the transformation using Basic logging and check the Logging tab for the results.
You can verify the basic logging information where you should see the following line:
2011/01/25 10:40:40 - Write books counting.0 - Book news = 8
Now, you will generate two Excel files and write the information about cheap and expensive books to the log.
price ' 50
. We will use an arbitrary price of $50 to determine if a book is cheap or expensive. cheapBooks.xls
, and use the Get Fields button to populate the grid under the Fields tab. qty
in the Aggregates grid, choose the field title_id
in the Subject column, and select the Number of Values (N) option in the Type column. line
of String
type in the UDJE step with the following Java expression:"Cheap books = " + Java.lang.Long.toString(qty)
Write books counting
in the step name column of the grid. line
field to the Fields grid and choose Basic logging
in the Log level listbox. Excel Output 2)
to generate the expensiveBooks.xls
file and replace the text Cheap
for Expensive
in the other UDJE step.Running the transformation using Basic logging, you can verify that your custom messages have been written to the log under the Logging tab. Here's an example:
The main objective in this recipe is to explain how you can write personalized messages to the logging windows. The task of the transformation is simple—it reads a text file at the beginning, uses a Filter rows step to split the list of books into cheap and expensive ones, and then writes two Excel spreadsheets with these details.
Now, let's analyze the task of customizing the log. In the first part of the recipe you wrote into the log the number of books:
After each of the Excel output steps, there is the same sequence of steps (Group by, UDJE, and Write to log), in order to write a message with the number of cheap books and the number of expensive books into the log.
There is also a Block this step until steps finish step in this sequence before the Write to log step, this is because you want to be sure that the total number of books will be written first.
This recipe showed you the classic way for adding messages to the log. The following subsections show you some variants or alternative approaches.
Instead of adding text to the log, you may want to filter text in the existing log.
If you select the Logging tab in the Execution Results window, then you will see a toolbar. Under that toolbar, there is a button named Log settings that allows you to apply a filter. If you type some text into the Select filter textbox, then the log of the subsequent executions of the transformation will only show the lines that contain that text. For example, you could use the same text prefix in all of your custom messages, and then apply a filter using this fixed text to see only those messages.
This also works if you run the transformation from a job, and even if you restart Spoon because the filter is saved in a Kettle configuration file.
This is valid only in Spoon. If you intend to run a job or a transformation with Pan or Kitchen and you need to filter a log, for example, by keeping only the lines that contain certain words, then you have to take another approach. One way for performing that would be to save the job or transformation log in a file and then run another transformation that parses and filters that log file.
The Logging window shows not only your messages, but also all of the information written by the steps. Sometimes you need a clean log file showing only your custom messages and discarding the rest.
There is another problem related to that - when you configure the Write to log step, you need to specify a level for your log. (In the recipe, you used Basic logging)
. If you run your transformation using a different log level, then you will not see any of your personalized messages.
One alternative would be using a Text file output instead of the Write to log step. With this, you will produce a new text file with only your desired messages. Be sure to point all of the Text file output steps to the same Filename under the File tab, and use the Append checkbox under the Content tab, in order to avoid overwriting the file with each run.
It is possible that you want to see different log levels depending on the job or transformation, or that you simply want to isolate the log for a particular job or transformation. This is a simple task to accomplish. In the main job, right-click on the Job or Transformation entry of interest; under the Logging settings tab check the Specify logfile? option and you will be able to specify a name for the log file as well as the log level desired. In this way, you can create different log files with different log levels for each of the Jobs and Transformations that are part of your main job.