Whereas Qlik provides ample system-wide logging, utility particular logging stays considerably inaccessible. Furthermore, it’s tough to configure and requires parsing to acquire what’s required. On this article, we’ll assemble a customized operate that leverages present Qlik Script logging. This may make message customization and retrieval easy, efficient, and reusable.
Logging is crucial to utility improvement
Builders know that suggestions whereas developing an utility is crucial. No utility is constructed with no few (or many!) errors and hurdles that have been unknown on the outset. The faster the time between problem identification and determination, the simpler improvement will be. That is the place logging is usually a crucial piece of the event course of.
There are a lot of causes to allow your functions to ship suggestions. However these two are arguably crucial:
- Establish errant information within the improvement cycle
- Word any present or potential points through the upkeep part
Preserving wanted messages in an accessible place will be the distinction in a single’s sanity. Whether or not that’s in a Qlik app or easy textual content logs. Relying on the messages created, you too can make logs obtainable to utility shoppers; they will present confidence that underlying processes are working as anticipated. For instance, they may discover out whether or not a given file loaded with the anticipated variety of rows or if sure discipline values have been current.
Leveraging Qlik script logs
Qlik offers many varieties of logging and no scarcity of data. Nevertheless, the logging messages, log location and quantity makes them lower than pleasant to sift by way of. By default, Qlik shops its logs at %ProgramDatapercentQlikSenseLog as configured by the native log configuration file. By loading up the focused data contained in these recordsdata after which programmatically sifting by way of it to establish related messages, we will output extra related and accessible log recordsdata to a customized locale.
As a result of our goal is to offer utility suggestions, we might be wanting solely on the script logs. Within the default set up, a Knowledge Supply Connection referred to as ’ServerLogFolder’ is made obtainable for log inquiry functions. Perusing the ‘Script’ folder by way of this information supply connection, we discover a multitude of cryptically labeled recordsdata. The names of those recordsdata correspond to the underlying utility identifier generally known as the DocumentName() – to not be confused with the DocumentTitle() which states the human-readable utility title.
Qlik log recordsdata
Understanding the placement, naming conference, and content material permits conceptual understanding of the best way to purchase suggestions messages for builders or shoppers. Nevertheless, there are a number of hurdles that now current themselves, not least of which is the quantity of log recordsdata obtainable (particularly in a bigger or getting older set up) in addition to the quantity of what might be thought of noise inside every log file.
A fast peek at one in every of these many recordsdata reveals numerous data that we simply received’t want frequently.
We will fairly successfully cut back the variety of recordsdata searched by solely contemplating these matching the applying GUID identifier – be aware the wildcard load clause for vLogID outlined within the connected file.
Supposing that we will load these logs right into a Qlik utility and filter them, we’ll rapidly discover the necessity to add particular data to this file. Thankfully, Qlik has offered the TRACE operate which not solely writes to the output window throughout logging but additionally to the Scripts log. Loading and filtering the log for customized statements are the foundational parts upon which we’ll construct our log tapping operate.
1: Including Log Messages by way of Hint
By merely including the next line to any Qlik utility, the present respective log file recognized by the DocumentName (or app GUID) will include the designated message.
TRACE My Log message;
If we lengthen our TRACE message with a constant and distinctive prefix, then we will filter the applying log for any of our messages.
SET vPrefix=mymsg;
TRACE $(vPrefix) - My Log message;
….
TRACE $(vPrefix) - Finish Software construct;
Now we will 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 outline the vLog enter as beneath to offer a run-time message inside the TRACE assertion. We’ll customise our log message by including variables for Consumer, Shopper, Software, a delimiter, and message Textual content. Discover that we use the SET as a substitute of LET command to allow the dynamic variable growth at run time.
SET vLog = '$(vDelim)$(vMsgPrefix)$(vDelim)$(vMsgUser)$(vDelim)$(vMsgClient)$(vDelim)$(vMsgApp)$(vDelim)$(vMsgText)';
Assigning these variables and structuring their storage and eventual output is programming that we need to do for each utility for which we need to deploy our logging operate. Subsequently, we’ll specify our message from inside the TRACE assertion with as little added effort as attainable utilizing this Dynamic Variable. The one enter we’d like at logging time is the message.
TRACE $(vLog);
By structuring the variable on this manner, we will now program the variable task with a sub-routine to assemble our message in no matter manner we wish by calling the dynamic variable.
3: Reuse Code with SubRoutines and MustInclude
We’re now prepared to start writing a selected operate to assemble our log. The tactic right here is to put in writing up a callable sub routine utilizing Qlik’s SUB operate. Subroutines should be positioned earlier than the script instructions during which they’re referred to as to allow them to be obtainable. You possibly can then invoke them with CALL when wanted.
To reveal, we’ll begin 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));
Utilizing this construction, the logging and log file era is coded and is made callable. The Log subroutine is beneath:
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 utility
We’ll make a brief departure now to debate abstracting this from any particular person Qlik utility right into a operate you possibly can leverage throughout functions with only a few instructions. Copying the code right into a textual content file and saving it to a Qlik accessible location permits us to INCLUDE our new operate inside any utility on our set up. Use this code:
$(Must_Include=lib://<<FILEPATH>>/fxnGenerateAppLog.txt);
//NOTE: <<FILEPATH>> represents the placement decided by file placement.
//HINT: If achieved inside an present information supply connection no further setup is required.
Combining the embrace and subroutine capabilities ensures – with a single line of code – that our variables are initialized, and our subroutines are outlined and obtainable for our use. That is the place we will set default values for Prefix, Delim, Shopper, and a vPurgeDays variable to outline message retention. You possibly can entry the entire code within the file beneath. Its utilization turns into easy.
$(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 utility to consolidate and write log file.
CALL GenerateAppLog;
Within the fxnGenerateAppLog.txt file you’ll discover variables and definitions, crucial of which to setup is the output file location, vLogOutput. Once more, utilizing an already outlined information supply location,you are able to do this simply with out further configuration. Nevertheless, you possibly can customise because the surroundings calls for. You possibly can customise variables AFTER the embrace assertion; this may have various results on log overwriting relying on how that is achieved. For instance, if log recordsdata are extra usefully stored by Shopper, the vMsgFile variable can merely be reset to vMsgClient. Constant use of the vMsgFile and vMsgPrefix will enable log consolidation.
Conclusion
By tapping Qlik’s default logging scheme and making use of the ideas of TRACE, Dynamic Variables, subroutines and embrace statements builders could make customizable logging options to help their improvement and client wants. Completely satisfied logging!
Obtain the entire code right here.
Maintain Studying: Again To The Fundamentals With Qlik >>