IBM i 7.6 Makes Searching Job Logs with SQL Even Better
Who hasn’t spent far too long wandering around a job log, searching for the one message that explains why everything has gone sideways?
You know the routine. Open the job log, scroll down, scroll back up, press Find, discover twelve occurrences of the word “program”, make a cup of tea, and eventually notice the escape message that was staring at you all along.
Fortunately, IBM i gives us a better option.
QSYS2.JOBLOG_INFO lets us treat a job log like a table. With IBM i 7.6, it has become even more useful thanks to new options for ordering messages, limiting the results and selecting messages by timestamp.
Put the kettle on. Let’s go job-log hunting.
Reading a job log with SQL
QSYS2.JOBLOG_INFO returns one row for each message in a job log. Give it a qualified job name and it gives you the information you would normally see through DSPJOBLOG.
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
from_program,
message_text
FROM TABLE(
QSYS2.JOBLOG_INFO('123456/USERNAME/JOBNAME')
) AS joblog
ORDER BY ordinal_position;
That is already more useful than a long scrolling display because we can filter, sort and search the results using ordinary SQL.
The qualified job name follows the usual format:
JOB-NUMBER/USER/JOB-NAME
For example:
SELECT *
FROM TABLE(
QSYS2.JOBLOG_INFO('347117/QUSER/QZDASOINIT')
) AS joblog;
Searching the current job
If you want the job executing the SQL statement, pass '*':
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
message_text
FROM TABLE(
QSYS2.JOBLOG_INFO('*')
) AS joblog
ORDER BY ordinal_position;
You can also use the QSYS2.JOB_NAME built-in global variable:
SELECT *
FROM TABLE(
QSYS2.JOBLOG_INFO(QSYS2.JOB_NAME)
) AS joblog;
QSYS2.JOB_NAME contains the qualified name of the job where the SQL statement runs.
There is one small wrinkle. If you run SQL through ACS or another database client, your statement may execute inside a database server job. In that case, “current job” means that server job. IBM i is being perfectly logical, which is not always the same thing as being helpful.
Searching for some useful text
Once the job log is a table, finding a message becomes pleasantly simple:
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
message_text
FROM TABLE(
QSYS2.JOBLOG_INFO('*')
) AS joblog
WHERE UPPER(message_text) LIKE '%OBJECT NOT FOUND%'
ORDER BY ordinal_position;
Using UPPER makes the search case-insensitive.
If you know the message identifier, search for that instead:
SELECT ordinal_position,
message_timestamp,
message_id,
severity,
message_text,
message_second_level_text
FROM TABLE(
QSYS2.JOBLOG_INFO('*')
) AS joblog
WHERE message_id = 'CPF9801'
ORDER BY ordinal_position;
You can also look specifically for diagnostic and escape messages:
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
from_program,
message_text
FROM TABLE(
QSYS2.JOBLOG_INFO('*')
) AS joblog
WHERE message_type IN ('DIAGNOSTIC', 'ESCAPE')
ORDER BY ordinal_position;
That usually removes a great deal of noise and gets you much closer to the interesting part.
I love this technique to search for a specific text-string in a joblog
Use this wild card search for something/anything in any jobs joblog.
In this example for my job 912960/NICKLITTEN/QPADEV0002 I entered “what is this” in the command line like this:
Of course this joblog could be huge so lets just search for these words like this:
The shiny new IBM i 7.6 options
This is where things get rather lovely.
With IBM i 7.6 and Db2 Group PTF SF99960 Level 3, IBM added three parameters to JOBLOG_INFO:
MESSAGE_ORDERMESSAGE_LIMITMESSAGE_TIMESTAMP
These let us reduce the amount of job-log data before SQL returns it.
Suppose I only want messages from the last 30 minutes, with the newest messages first:
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
message_text
FROM TABLE(
QSYS2.JOBLOG_INFO(
'*',
MESSAGE_ORDER => 'DESCENDING',
MESSAGE_TIMESTAMP => CURRENT TIMESTAMP - 30 MINUTES
)
) AS joblog
ORDER BY ordinal_position;
That is considerably nicer than retrieving a job log containing three days of activity and filtering it afterwards.
The MESSAGE_ORDER choices are:
ASCENDING
DESCENDING
ASCENDING returns the oldest message first and remains the default. DESCENDING returns the newest message first.
When descending order is used, ORDINAL_POSITION 1 represents the newest message returned.
Limiting the number of messages
Sometimes I only want the latest few messages:
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
message_text
FROM TABLE(
QSYS2.JOBLOG_INFO(
'*',
MESSAGE_ORDER => 'DESCENDING',
MESSAGE_LIMIT => 100
)
) AS joblog
ORDER BY ordinal_position;
This returns no more than 100 rows.
We can combine all three new parameters:
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
from_program,
message_text
FROM TABLE(
QSYS2.JOBLOG_INFO(
'*',
MESSAGE_ORDER => 'DESCENDING',
MESSAGE_LIMIT => 200,
MESSAGE_TIMESTAMP => CURRENT TIMESTAMP - 30 MINUTES
)
) AS joblog
WHERE message_type IN ('DIAGNOSTIC', 'ESCAPE')
ORDER BY ordinal_position;
Now we are asking IBM i for:
- Messages written during the last 30 minutes
- Newest messages first
- No more than 200 messages
- Only diagnostic and escape messages in the final result
That is a much more focused view of what the job has been complaining about.
Showing messages around the first match
Sometimes the message you find is only the beginning of the story. You need to see what happened immediately before and after it.
JOBLOG_INFO already provides ORDINAL_POSITION, so we can use that to display the surrounding messages:
WITH joblog AS (
SELECT joblog.*,
MIN(
CASE
WHEN UPPER(message_text) LIKE '%OBJECT NOT FOUND%'
THEN ordinal_position
END
) OVER () AS first_match
FROM TABLE(
QSYS2.JOBLOG_INFO('*')
) AS joblog
)
SELECT ordinal_position,
message_timestamp,
message_id,
message_type,
severity,
message_text
FROM joblog
WHERE first_match IS NOT NULL
AND ordinal_position
BETWEEN first_match - 5 AND first_match + 20
ORDER BY ordinal_position;
This returns five messages before the first match and twenty messages after it.
Adjust those numbers to suit your level of curiosity and the size of the disaster.
First-level and second-level message text
Two particularly useful columns are:
MESSAGE_TEXT
MESSAGE_SECOND_LEVEL_TEXT
MESSAGE_TEXT contains the first-level message text. MESSAGE_SECOND_LEVEL_TEXT contains the longer explanation you would normally reach by displaying the message details.
Both are Unicode VARGRAPHIC values with CCSID 1200. Current versions of ACS should display them correctly.
If your SQL client makes the text look peculiar, you can convert it to UTF-8:
SELECT CAST(
message_text
AS VARCHAR(4096) CCSID 1208
) AS message_text,
CAST(
message_second_level_text
AS VARCHAR(16384) CCSID 1208
) AS second_level_text
FROM TABLE(
QSYS2.JOBLOG_INFO('*')
) AS joblog;
Do not automatically convert the text to a language-specific EBCDIC CCSID. Keeping it as Unicode, or converting it to UTF-8, gives you a much better chance of preserving every character.
A couple of useful extra columns
IBM has continued improving the results returned by JOBLOG_INFO.
Alongside the familiar message information, current systems provide:
MESSAGE_KEY, containing the system-assigned message keyQUALIFIED_JOB_NAME, containing the complete job nameMESSAGE_TOKENS, now supporting up to 12,000 bytes on current IBM i 7.6 levels
Other useful columns include FROM_PROGRAM, FROM_PROCEDURE, TO_PROGRAM, MESSAGE_FILE and MESSAGE_LIBRARY.
If you are building support tooling, automated diagnostics or a job-log viewer, there is plenty here to work with.
Remember the authority rules
You cannot use SQL to sneak into job logs you are not authorised to see. IBM i has opinions about that, and quite rightly so.
To read another job’s log, your profile must normally meet one of these conditions:
- Your user profile matches the target job’s user identity.
- You have
*JOBCTLspecial authority.
Extra restrictions apply when the target job’s user identity has *ALLOBJ. The caller then needs *ALLOBJ or authority to the QIBM_ACCESS_ALLOBJ_JOBLOG function usage identifier.
Check your Db2 Group PTF level
The new IBM i 7.6 parameters arrived with Db2 Group PTF SF99960 Level 3.
If MESSAGE_ORDER, MESSAGE_LIMIT or MESSAGE_TIMESTAMP produces an error, check the installed group PTF level before glaring suspiciously at the SQL. Your query may be ready for the future while the partition is still catching up.
IBM documents the current function and its enhancement history here:
Job-log searching without the scrolling marathon
QSYS2.JOBLOG_INFO has always been one of those quietly useful IBM i services. IBM i 7.6 makes it substantially better.
We can now ask for the newest messages first, limit the number of rows and ignore everything older than a chosen timestamp. Add ordinary SQL predicates for message type, severity, program, message identifier or text, and a huge job log becomes a small, useful result set.
That means less scrolling, less hunting and more time fixing the actual problem.
Which, if we are lucky, leaves enough time to drink the tea while it is still warm.


