PDA

View Full Version : All-purpose reusable logger unit



DirectuX
03-01-2020, 19:43
Hi,

I wrote a little unit for logging purposes.

All comments welcome.

https://github.com/DirectuX/logger

Sample and documentation soon.

Petr Schreiber
03-01-2020, 23:10
Sebastian,

thanks a lot for working on this!

I noticed two things:
- the LOG type function collides with LOG keyword in thinBASIC (I get "Function name is a reserved key") for this example:


#include "https://raw.githubusercontent.com/DirectuX/logger/master/LOG.tbasicu"


dim logger as tLogThis


logger.log("Hello")


- from other programming languages I am used to concept of different log severities. For example debug, info, warning, error - not sure how to apply it here

I do appreciate you implement advanced stuff like log rotation :drink:


Petr

DirectuX
04-01-2020, 12:34
Thank you for the feedback Petr.

I'll take it into account and adjust the code soon. I've choose 'Log' because I wanted it to be short. Will rename.

About severity, I have just implemented the 'important' flag which brighten the text colour in the console. Indeed I have seen these levels (debug, info, warning, error) before. I'll improve the code in this direction.

Log rotation is to complete, currently log file splits to a new one after maxSize and backup the previous. It lacks the old file deleting part. I'm still thinking about it. If to use the new FSO_DeleteByAge (https://www.thinbasic.com/public/products/thinBasic/help/html/fso_deletebyage.htm) function ? But what bothers me is that you can't delete to the trash. I would prefer a move to trash rather than delete.

What do you think about these two behaviour ?


Manage log rotation by size : after n files split, delete the oldest. n grows and remain constant.
Manage log rotation by date : log files are deleted after a fixed time.




Sebastian,

thanks a lot for working on this!

I noticed two things:
- the LOG type function collides with LOG keyword in thinBASIC (I get "Function name is a reserved key") for this example:


#include "https://raw.githubusercontent.com/DirectuX/logger/master/LOG.tbasicu"


dim logger as tLogThis


logger.log("Hello")


- from other programming languages I am used to concept of different log severities. For example debug, info, warning, error - not sure how to apply it here

I do appreciate you implement advanced stuff like log rotation :drink:


Petr

ErosOlmi
04-01-2020, 18:10
:D what a coincidence: I was working on a thinBasic module for logging.
A kind of all in one purpose able to log on console/file/syslog/database/WS/...

At office I have a lot of thinBasic scripts doing many different things (from data ETL to SFTP to Web Service calling and other things) and each has its own logging system.
I wanted a centralized log to being able to analyze them all in one admin console.

I was starting from SysLog protocol https://tools.ietf.org/html/rfc5424
and was testing using https://syslogwatcher.com/

Inspired by https://nlog-project.org/

Actually I'm studying rfc5424 syslog protocol

Will share soon.

ErosOlmi
04-01-2020, 18:29
If to use the new FSO_DeleteByAge (https://www.thinbasic.com/public/products/thinBasic/help/html/fso_deletebyage.htm) function ? But what bothers me is that you can't delete to the trash. I would prefer a move to trash rather than delete.





Good point. Will check what I can do.

DirectuX
04-01-2020, 19:33
Hi Eros,

that's a shame we were not aware each other of the work in progress. Now, I wonder the point with this unit if you're baking a module ?


A kind of all in one purpose able to log on console/file/syslog/database/WS/...

That's the point of this unit ( target is : console / file / msgbox / statusbar / richedit ).


I was starting from SysLog protocol

I've just started improving the code in this direction since Petr's feedback (severity advice).

ErosOlmi
04-01-2020, 19:54
I think it is worth to go on in parallel because we are working on similar matters but in different directions.

My intent for the moment is to have a Syslog thinBasic compiled module only working for myself in the company.
It will work using UDP at first and then TCP.
When mature and working fine I will release as source code in Github.

Instead you are making a great thinBasic source code library easy to be used and studied by anyone willing to develop using thinBasic

DirectuX
04-01-2020, 21:15
ok :)

I've already made the modifications I talked previously here (severity and reserved keyword conflict). Now writing a little demo.

Also, I'm working with Resources_Test001.tbasic sample on understanding the workflow with #ressources, DIALOG NEW and callbacks. It is not obvious for me at the moment.

ErosOlmi
04-01-2020, 21:45
Also, I'm working with Resources_Test001.tbasic sample on understanding the workflow with #ressources, DIALOG NEW and callbacks. It is not obvious for me at the moment.

Fault is from my side because I'm still experimenting and didn't give any advice on how to use.
Anyway if you right click on line

#resource ".\Resources_Test001.rc"
you should get menu with "Open <resouce file name>"
If you choose it a resource editor should open and you can edit/create forms
.RC file is a common development file format used in some programming languages like C
Forms and controls names defined in .RC file are than recognized at runtime by thinBasic engine and you can use in script.
Not all controls are still developed, only few of them.

I'm still try to find right direction.
But better to spin off and talk into another thread.

DirectuX
04-01-2020, 21:48
Thanks Eros,

I'll use it for the statusbar demo.

DirectuX
04-01-2020, 23:49
Hi,

while writing the demo I found that the switch function isn't doing well (https://www.thinbasic.com/community/project.php?do=gotonote&issuenoteid=3133) with udt variables.

Anyway, here (https://github.com/DirectuX/logger/tree/feat_severity)is the last version that raise an error at switch keyword.

Eros, do you think it is worth waiting for a fix or to workaround that in the meantime ?

DirectuX
05-01-2020, 13:33
Hi,

while writing the demo I found that the switch function isn't doing well (https://www.thinbasic.com/community/project.php?do=gotonote&issuenoteid=3133) with udt variables.

Anyway, here (https://github.com/DirectuX/logger/tree/feat_severity)is the last version that raise an error at switch keyword.

Eros, do you think it is worth waiting for a fix or to workaround that in the meantime ?

Very curiously, workaround is EASY (https://www.thinbasic.com/community/project.php?issueid=616).

DirectuX
05-01-2020, 22:58
Well,

fix_feat_severity (https://github.com/DirectuX/logger/tree/fix_feat_severity) is now the latest version.

Every functionality is now working thanks to workarounds. (errors reported (https://www.thinbasic.com/community/project.php))

There is a demo too.

Petr Schreiber
06-01-2020, 22:20
Hi Sebastian,

thanks a lot for the additions. I really appreciate you integrated the changed, thank you!

If I may, few ideas - not requests - just ideas.

When I see line like this:


dim logger as tLogthis(%TRUE, %TRUE, %FALSE, %FALSE)


... I feel a bit anxious - what was the first boolean flag, what second, what third, what fourth? This is why I would recommend to solve situation like this with a single binary mask instead.

Also, I noticed that the parameter #1, isDebug, contradicts parameter #4, verbose.

From "code smell" perspective, it is recommended to avoid negative flags. Instead of doNotPrependTag, I would preffer prependTag. This was mentioned in Code Complete (https://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670), it is a really nice book with lot of good advices.

I would suggest to not overdo it with parameters in constructor, and provide convenient defaults, overridable by methods:


dim logger as tLogthis()
logger.showConsole(true)
logger.setOutputMode(%LOG_MODE_DEBUG) # or %LOG_MODE_VERBOSE
logger.prependTag(false)


Too much typing? Yes, but you do it once, for setup. Readability? In my opinion much higher than %TRUE, %TRUE, %FALSE, %FALSE.

Last but not least, I think .logThis could be replaced with .write, it is a bit shorter and does not look too verbose.

Let me know what you think.


Petr

DirectuX
06-01-2020, 22:36
Hi Petr,,


Last but not least, I think .logThis could be replaced with .write, it is a bit shorter and does not look too verbose.

Yes ! Thanks !


Instead of doNotPrependTag, I would preffer prependTag

I perfectly agree, but then I have to take time to think how, as I wanted prependTag by optional default. Did you have something in mind ?


When I see line like this:


dim logger as tLogthis(%TRUE, %TRUE, %FALSE, %FALSE)

I feel a bit anxious - what was the first boolean flag, what second, what third, what fourth?

Here again I agree.
1/ Not an excuse but it may help understand my bad habit: with VB6, there always was a filtered auto suggest list along typing : so not room for error or questioning parameters.
2/ Did you just suggest what I did for this part ? :




'logWhere equates
%LT_Debug = 1 as Long 'Display only if isDebug = %TRUE
%LT_Time = 2 as Long ' Prepend DateTime$
%LT_ToConsole = 4 as Long
%LT_ToConsoleTitle = 8 as Long
%LT_ToMsgBox = 16 as Long
%LT_ToLogFile= 32 as long
%LT_ToStatusBar= 64 as long

(...)

function logThis(logWhere as long = 0, (...)

if Bit_Get(logWhere,1) then




if yes, that's ok and easy (readability part) but
for code writing, it remains the same until we do something for code auto suggestions.

Did I understand your idea ?




Hi Sebastian,

thanks a lot for the additions. I really appreciate you integrated the changed, thank you!

If I may, few ideas - not requests - just ideas.

When I see line like this:


dim logger as tLogthis(%TRUE, %TRUE, %FALSE, %FALSE)


... I feel a bit anxious - what was the first boolean flag, what second, what third, what fourth? This is why I would recommend to solve situation like this with a single binary mask instead.

How?

Compare the readability of this:


dim logger as tLogthis(%LOG_DEBUG | %LOG_SHOWCONSOLE)


Also, from "code smell" perspective, it is recommended to avoid negative flags. Instead of doNotPrependTag, I would preffer prependTag. This was mentioned in Code Complete (https://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670), it is a really nice book with lot of good advices.

Last but not least, I think .logThis could be replaced with .write, it is a bit shorter and does not look too verbose.

Let me know what you think.


Petr

Petr Schreiber
06-01-2020, 22:43
I perfectly agree, but then I have to take time to think how, as I wanted prependTag by optional default. Did you have something in mind ?

You can make prependTag OPTIONAL, but you can still give the parameter default value via `= true` or `= false`.


Not an excuse but it may help understand my bad habit: with VB6, there always was a filtered auto suggest list along typing : so not room for error or questioning parameters.
I understand your point with filling parameters, but it still is and issue when reading the code :)


Did you just suggest what I did for this part ?
Yes, but I then changed my mind because of the exclusivity between isDebug and verbose. Please see the edited post.


Thank you,
Petr

DirectuX
07-01-2020, 12:03
Hi,

@Petr, thank you again for your guidance :)

Master is now again the most up to date version of this Log unit. (https://github.com/DirectuX/logger)

To do: documentation, thus code is self explaining ( demo is present ).

Petr Schreiber
07-01-2020, 21:20
Thank you for the adjustments, Sebastian :)

It is looking good! I think we have a solid, highly customizable base here.

For quick logging, I would maybe like to have something like:


dim logger as tLogThis()
logger.SetDefaultLogTarget(%LT_ToMsgBox + %LT_ToConsole) ' If not specified other, these would be used. Stores parameter to me.defaultLogTarget internally
logger.SetDefaultTitle("Main") ' If not specified other, this title would be used. Stores parameter to me.defaultTitle internally


logger.wInfo("Ciao, Sebastian!") ' Internally calls logger.write(me.defaultLogTarget, "Ciao, Sebastian", me.defaultTitle, %TAG_INFO)
logger.wError("Ooops, problem happened") ' Internally calls logger.write(me.defaultLogTarget, "Ooops, problem happened", me.defaultTitle, %TAG_ERROR)


What do you think?

Petr

DirectuX
07-01-2020, 21:46
Great idea Petr !

As I'm already on another thinBasic project, I logged the idea (https://github.com/DirectuX/logger/issues) :D and be back soon on this.

ErosOlmi
07-01-2020, 23:32
If you can add "thinBasic" word somewhere in https://github.com/DirectuX/logger readme.md it will be found when people search for thinBasic.

Thanks

DirectuX
08-01-2020, 10:16
Done. Done the same for thinternational (https://github.com/DirectuX/thinternational).

DirectuX
10-01-2020, 12:47
Hi,

suggestion #2 (https://github.com/DirectuX/logger/issues/2) implemented.

Logger unit last version here. (Branch:master) (https://github.com/DirectuX/logger) :)

Petr Schreiber
11-01-2020, 22:00
Sebastian,

thank you for all the changes. I dared to perform a stylistic review here:
https://github.com/DirectuX/logger/pull/3/files

Before you have a peek - please, feel free to reject this change proposal, but I would suggest to simply have a look.

It can reveal you some thinBASIC secrets :) and also reliable way, how to work with binary flags.


Petr

DirectuX
13-01-2020, 14:39
Thanks to Petr,
there is a new logger version (https://github.com/DirectuX/logger) with legibility improved and minor corrections.