Whilst Qlik gives plentiful system-wide logging, software explicit logging stays fairly inaccessible. Additionally, it’s tricky to configure and calls for parsing to procure what is needed. On this article, we’ll assemble a customized serve as that leverages present Qlik Script logging. This will likely make message customization and retrieval easy, efficient, and reusable.
Logging is important to software building
Builders know that comments whilst developing an software is important. No software is constructed with out a couple of (or many!) mistakes and hurdles that have been unknown on the outset. The faster the time between factor identity and backbone, the more practical building can also be. That is the place logging is usually a essential piece of the advance procedure.
There are lots of causes to permit your programs to ship comments. However those two are arguably crucial:
- Determine errant information within the building cycle
- Observe any present or doable problems all the way through the upkeep segment
Holding wanted messages in an obtainable position can also be the variation in a single’s sanity. Whether or not that’s in a Qlik app or easy textual content logs. Relying at the messages created, you’ll additionally make logs to be had to software shoppers; they may be able to supply self assurance that underlying processes are running as anticipated. For instance, they might in finding out whether or not a given report loaded with the predicted choice of rows or if sure box values have been provide.
Leveraging Qlik script logs
Qlik supplies many sorts of logging and no scarcity of knowledge. Alternatively, the logging messages, log location and quantity makes them not up to pleasant to sift via. By way of default, Qlik shops its logs at %ProgramDatapercentQlikSenseLog as configured by way of the native log configuration report. By way of loading up the focused knowledge contained in those information after which programmatically sifting via it to spot related messages, we will be able to output extra related and obtainable log information to a customized locale.
As a result of our objective is to supply software comments, we can be taking a look completely on the script logs. Within the default set up, a Knowledge Supply Connection referred to as ’ServerLogFolder’ is made to be had for log inquiry functions. Perusing the ‘Script’ folder by way of this knowledge supply connection, we discover a multitude of cryptically categorized information. The names of those information correspond to the underlying software identifier referred to as the DocumentName() – to not be puzzled with the DocumentTitle() which states the human-readable software title.
Qlik log information
Working out the site, naming conference, and content material permits conceptual working out of tips on how to gain comments messages for builders or shoppers. Alternatively, there are a number of hurdles that now provide themselves, now not least of which is the amount of log information to be had (particularly in a bigger or ageing set up) in addition to the volume of what might be thought to be noise inside of every log report.
A snappy peek at this sort of many information finds numerous knowledge that we simply received’t want regularly.

We will be able to rather successfully scale back the choice of information searched by way of most effective making an allowance for the ones matching the applying GUID identifier – be aware the wildcard load clause for vLogID outlined within the hooked up report.
Supposing that we will be able to load those logs right into a Qlik software and clear out them, we can briefly in finding the want to upload explicit knowledge to this report. Thankfully, Qlik has equipped the TRACE serve as which now not most effective writes to the output window all the way through logging but additionally to the Scripts log. Loading and filtering the log for customized statements are the foundational elements upon which we’ll construct our log tapping serve as.
1: Including Log Messages by way of Hint
By way of merely including the next line to any Qlik software, the present respective log report recognized by way of the DocumentName (or app GUID) will comprise the designated message.
TRACE My Log message;
If we lengthen our TRACE message with a constant and distinctive prefix, then we will be able to clear out the applying log for any of our messages.
SET vPrefix=mymsg;
TRACE $(vPrefix) - My Log message;
….
TRACE $(vPrefix) - Finish Utility construct;
Now we will be able to utterly customise our message with variables and ship it to the TRACE command.
2: Leverage dynamic variables to specify customized message
A dynamic variable is assigned programmatically on the time it’s run. We will be able to outline the vLog enter as under to supply a run-time message throughout the TRACE commentary. We’ll customise our log message by way of including variables for Consumer, Shopper, Utility, a delimiter, and message Textual content. Realize that we use the SET as an alternative of LET command to permit the dynamic variable enlargement at run time.
SET vLog = '$(vDelim)$(vMsgPrefix)$(vDelim)$(vMsgUser)$(vDelim)$(vMsgClient)$(vDelim)$(vMsgApp)$(vDelim)$(vMsgText)';
Assigning those variables and structuring their garage and eventual output is programming that we need to do for each and every software for which we need to deploy our logging serve as. Subsequently, we’ll specify our message from throughout the TRACE commentary with as little added effort as imaginable the use of this Dynamic Variable. The one enter we want at logging time is the message.
TRACE $(vLog);
By way of structuring the variable on this means, we will be able to now program the variable task with a sub-routine to build our message in no matter means we would like by way of calling the dynamic variable.
3: Reuse Code with SubRoutines and MustInclude
We’re now in a position to start writing a selected serve as to build our log. The process this is to write down up a callable sub habitual the use of Qlik’s SUB serve as. Subroutines want to be positioned ahead of the script instructions wherein they’re referred to as so they may be able to be to be had. You’ll then invoke them with CALL when wanted.
To reveal, we’ll get started with a easy variable task for the App GUID and a log message.
SUB GenerateAppLog
LET vLogID=DocumentName()&' - $1';
END SUB;
CALL GenerateAppLog;
TRACE $(vLogID(My Log message));
The usage of this construction, the logging and log report era is coded and is made callable. The Log subroutine is under:
SUB LOG (vMsgText)
SET vLog = '$(vDelim)$(vMsgPrefix)$(vDelim)$(vMsgUser)$(vDelim)$(vMsgClient)$(vDelim)$(vMsgApp)$(vDelim)$(vMsgText)';
TRACE $(vLog);
END SUB;
CALL LOG(‘Insert Message Right here’);
Abstracting from a Qlik software
We’ll make a brief departure now to talk about abstracting this from anyone Qlik software right into a serve as you’ll leverage throughout programs with only a few instructions. Copying the code right into a textual content report and saving it to a Qlik obtainable location permits us to INCLUDE our new serve as inside of any software on our set up. Use this code:
$(Must_Include=lib://<<FILEPATH>>/fxnGenerateAppLog.txt);
//NOTE: <<FILEPATH>> represents the site made up our minds by way of report placement.
//HINT: If finished inside of an present information supply connection no further setup is needed.
Combining the come with and subroutine purposes guarantees – with a unmarried line of code – that our variables are initialized, and our subroutines are outlined and to be had for our use. That is the place we will be able to set default values for Prefix, Delim, Shopper, and a vPurgeDays variable to outline message retention. You’ll get entry to your complete code within the report under. Its utilization turns into simple.
$(Must_Include=lib://<<FILEPATH>>/fxnGenerateAppLog.txt);
//Optionally customise vMsgXXXX variables
//LET vMsgFile = $(vMsgClient);
//LET vMsgPrefix = 'ABC';
CALL LOG ('Insert Message Right here');
//Positioned at finish of software to consolidate and write log report.
CALL GenerateAppLog;
Within the fxnGenerateAppLog.txt report you’ll in finding variables and definitions, crucial of which to setup is the output report location, vLogOutput. Once more, the use of an already outlined information supply location,you’ll do that simply with out further configuration. Alternatively, you’ll customise as the surroundings calls for. You’ll customise variables AFTER the come with commentary; this may occasionally have various results on log overwriting relying on how that is finished. For instance, if log information are extra usefully stored by way of Shopper, the vMsgFile variable can merely be reset to vMsgClient. Constant use of the vMsgFile and vMsgPrefix will permit log consolidation.
Conclusion
By way of tapping Qlik’s default logging scheme and making use of the ideas of TRACE, Dynamic Variables, subroutines and come with statements builders could make customizable logging answers to fortify their building and shopper wishes. Glad logging!
Obtain your complete code right here.
Stay Studying: Again To The Fundamentals With Qlik >>