Library "log" Logging library for easily displaying debug, info, warn, error and critical messages.
No real need to explain why you might want to use this library! I'm sure you've all experienced the frustration of trying to understand the data state of your scripts... so, enjoy! More on it's way...
(Don't forget to check the helpers in the script and the useful tips below)
Some Useful Tips
By default the log console persists between bars (for history) and bars and ticks (for realtime). Sometimes it is useful to clear the log after each candle or tick (assuming we are using the above helpers):
``` log_print(clear = true) // starts afresh on every bar and tick (excludes historical bars but good realtime tick analysis) log_print(clear = barstate.isnew) // clears the log at the start of each bar (again, excludes historical but good realtime candle analysis) ```
It is also useful to be able to selectively understand the state of data at specific points or times within a script:
``` if log.once() debug('useful variable', my_var) // this log only gets written once, upon first execution of this statement
if log.only(5) debug3(a, b, c) // these variables are only logged the first five times this statement is executed
log_print(clear = false) // clear must be false and you should not write other logs on every bar, or the above will be lost ```
Final tip. If you want to view ONLY log entries of a particular level, then negate the constant:
``` log_print(level = -LOG_DEBUG) ```
Detailed Interface
once() Restrict execution to only happen once. Usage: if assert.once()\n happens_once() Returns: bool, true on first execution within scope, false subsequently
only(repeat) Restrict execution to happen a set number of times. Usage: if assert.only(5)\n happens_five_times() Parameters: repeat: int, the number of times to return true Returns: bool, true for the set number of times within scope, false subsequently
init() Initialises the log array Returns: string[], tuple based array to contain all pending log entries (__LOG)
clear(msgs) Clears the log array Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG)
trace(msgs, msg) Writes a trace message to the log console Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) msg: string, the trace message to write to the log
debug(msgs, msg) Writes a debug message to the log console Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) msg: string, the debug message to write to the log
info(msgs, msg) Writes an info message to the log console Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) msg: string, the info message to write to the log
warn(msgs, msg) Writes a warning message to the log console Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) msg: string, the warn message to write to the log
error(msgs, msg) Writes an error message to the log console Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) msg: string, the error message to write to the log
fatal(msgs, msg) Writes a critical message to the log console Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) msg: string, the fatal message to write to the log
log(msgs, level, msg) Write a log message to the log console with a custom level Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) level: ing, the logging level to assign to the message msg: string, the log message to write to the log
severity(msgs) Checks the unprocessed log messages and returns the highest present level Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) Returns: int, the highest level found within the unfiltered logs
print(msgs, level, clear, rows, text_size, position) Prints all log messages to the screen Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) level: int, the minimum required log level of each message to be displayed clear: bool, clear the printed log console after each render (useful with realtime when set to barstate.isconfirmed) rows: int, the number of rows to display in the log console text_size: string, the text size of the log console (global size vars) position: string, the position of the log console (global position vars)
unittest_log(case) Log module unit tests, for inclusion in parent script test suite. Usage: log.unittest_log(__ASSERTS) Parameters: case: string[], the current test case and array of previous unit tests (__ASSERTS)
unittest(verbose) Run the log module unit tests as a stand alone. Usage: log.unittest() Parameters: verbose: bool, optionally disable the full report to only display failures
Note di rilascio
⋅
v2 - Added disabling of automatic line numbers. Fixed mistakes in helpers.
Updated: print(msgs, level, clear, rows, text_size, position, line_numbers) Prints all log messages to the screen Parameters: msgs: string[], the current collection of unfiltered and unprocessed logs (__LOG) level: int, the minimum required log level of each message to be displayed clear: bool, clear the printed log console after each render (useful with realtime when set to barstate.isconfirmed) rows: int, the number of rows to display in the log console text_size: string, the text size of the log console (global size vars) position: string, the position of the log console (global position vars) line_numbers: bool, disable the automatic prepending of line numbers
@HeWhoMustNotBeNamed, Thank you kindly! Keep an eye out, I have a lot more libraries to share (and then I might get time to polish up some indicators, ha)
lesdieuxdelacrypto
⋅
Thank you for this, is it possible to use it in a strategy?
GeoffHammond
⋅
@lesdieuxdelacrypto, Yes, this library works within strategies. I've also corrected the inaccurate HELPER message as per your prior comments, thank you for spotting that.
IkinovInvest
⋅
That's looks awesome !
But I put import GeoffHammond/log/2 and then try __LOG = init() but doens't works, how to import ?
Thank you, Pinescript is so haaaaaaaard
GeoffHammond
⋅
@GhostInTheShellTwenty, So, firstly, have a look at the HELPERS section as this may help, however, it's failing because you need to prepend the library name: "__LOG = log.init()", the code within the library has access to the native functions directly, but when used as an imported library you must access the functions through the scope of the library, thus requiring the "log.<function name>" prepend. As mentioned, I have example functions in the HELPERS section that you can add to your script, which act as wrappers to the library so you don't have to bother with the prepend or with passing in the "__LOG" variable.
IkinovInvest
⋅
@GeoffHammond, well I am sorry I really try to understand. Here I try to put a test msg on screen : __LOG = log.init() msg = "test" info(msg, float val = na) => log.info(__LOG, str.tostring(msg) + (not na(val) ? str.format(': "{0}"', val) : '')) where i am wrong ?
GeoffHammond
⋅
@GhostInTheShellTwenty, here is a working example for you with some comments: ---
log.info(__LOG, msg) // direct to library info(msg) // using the helper info(msg, 1) // using helper with label and number / numeric variable
plot(close)
log.print(__LOG) // direct call to library print to print the table // log_print(clear = true) // alternative call to print using helper, that clears the table after each tick (try them both depending on your needs)
IkinovInvest
⋅
@GeoffHammond, Well thank you for the example ! I created an indicator and put your code in and it's works just fine :) I lost a lot of time by debuging because the problem was the fact that I was using STRATEGY >< This library is wonderfull to know the values of variable on indicators ! I don't know why pinescript didn't have a console.log or something like that (like javascript) or var_dump like PHP.
lesdieuxdelacrypto
⋅
Hello @GeoffHammond, this library seems like it's exactly what I need but I cant get it to work. I created an INDICATOR and have copy pasted this example and I get the following error:
line 10: Cannot call 'log.print' with 'na' as a value for a non-typified argument. The argument 'position' should be explicitly typified.