-
Notifications
You must be signed in to change notification settings - Fork 28
Add log library based on log4p #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d3c0b2f
Initial logger library + example
ngeiswei d6c2ae7
Add comments to lib_logger.pl
ngeiswei d6e20dc
Implement main logging functions
ngeiswei fdd9d4e
Export main logging functions to MeTTa
ngeiswei a0753e4
Add tests of main logging functions
ngeiswei 35dd28b
Complete logger library
ngeiswei 70d9722
Complete logger library tests
ngeiswei 996d00b
Add comment about installation
ngeiswei 859f4bb
Remove useless NEXT message
ngeiswei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| ;; Test logger library. | ||
| ;; | ||
| ;; You may need to install log4p by running the following command: | ||
| ;; | ||
| ;; ``` | ||
| ;; swipl pack install log4p | ||
| ;; ```` | ||
|
|
||
| ;; Import logger library | ||
| !(import! &self (library lib_logger)) | ||
|
|
||
| ;; Get current filepath | ||
| !(test | ||
| (logger-filepath) | ||
| "petta.log") | ||
|
|
||
| ;; Get current stdout flag | ||
| !(test | ||
| (logger-stdout) | ||
| False) | ||
|
|
||
| ;; Get current timestamp flag | ||
| !(test | ||
| (logger-timestamp) | ||
| True) | ||
|
|
||
| ;; Get current log-level | ||
| !(test | ||
| (logger-level) | ||
| info) | ||
|
|
||
| ;; Get all possible log levels | ||
| !(test | ||
| (logger-levels) | ||
| (trace debug info warn error fatal)) | ||
|
|
||
| ;; Set log level to debug | ||
| !(test | ||
| (logger-set-level debug) | ||
| True) | ||
|
|
||
| ;; Get current log-level | ||
| !(test | ||
| (logger-level) | ||
| debug) | ||
|
|
||
| ;; Log message at the trace level (should be ignored) | ||
| !(test | ||
| (logger-trace "Log message at the trace level") | ||
| True) | ||
|
|
||
| ;; Log message at the debug level | ||
| !(test | ||
| (logger-debug "Log message at the debug level") | ||
| True) | ||
|
|
||
| ;; Log message at the info level | ||
| !(test | ||
| (logger-info "Log message at the info level") | ||
| True) | ||
|
|
||
| ;; Log message at the warn level | ||
| !(test | ||
| (logger-warn "Log message at the warn level") | ||
| True) | ||
|
|
||
| ;; Log message at the error level | ||
| !(test | ||
| (logger-error "Log message at the error level") | ||
| True) | ||
|
|
||
| ;; Log message at the fatal level | ||
| !(test | ||
| (logger-fatal "Log message at the fatal level") | ||
| True) | ||
|
|
||
| ;; Log message at the given info level | ||
| !(test | ||
| (logger-log info "Log message at the given info level") | ||
| True) | ||
|
|
||
| ;; Log message containing MeTTa terms at the given info level | ||
| !(test | ||
| (logger-logf info | ||
| "Log terms %w, %w and %w at the given info level" | ||
| ((+ 21 21) "Hello World!" (R A B))) | ||
| True) | ||
|
|
||
| ;; Log message containing MeTTa terms at the info level | ||
| !(test | ||
| (logger-infof "Log terms %w, %w and %w at the info level" | ||
| ((+ 21 21) "Hello World!" (R A B))) | ||
| True) | ||
|
|
||
| ;; Reset log filepath | ||
| !(test | ||
| (logger-set-filepath "my-petta.log") | ||
| True) | ||
| !(test | ||
| (logger-info "Log message to \"my-petta.log\" file") | ||
| True) | ||
|
|
||
| ;; Enable logging to stdout | ||
| !(test | ||
| (logger-set-stdout True) | ||
| True) | ||
| !(test | ||
| (logger-info "Message to stdout as well") | ||
| True) | ||
|
|
||
| ;; Disable prefixing the message with timestampe | ||
| !(test | ||
| (logger-set-timestamp False) | ||
| True) | ||
| !(test | ||
| (logger-info "Message without timestamp") | ||
| True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| ;; Logger library to log timestamped messages into a log file. New | ||
| ;; messages never replace old messages, instead they are appended to | ||
| ;; the end of the log file. The responsability of deleting a log file | ||
| ;; is left the user. | ||
| ;; | ||
| ;; This logger library is based on | ||
| ;; [log4p](https://www.swi-prolog.org/pack/list?p=log4p). To install | ||
| ;; log4p you may need to run the following command: | ||
| ;; | ||
| ;; ``` | ||
| ;; swipl pack install log4p | ||
| ;; ``` | ||
| ;; | ||
| ;; The logger library contains the following methods: | ||
| ;; | ||
| ;; - (logger-filepath) returns a string of the filepath where log | ||
| ;; messages are written. Default: "petta.log". | ||
| ;; | ||
| ;; - (logger-stdout) returns True iff messages are written to stdout. | ||
| ;; Default: False. | ||
| ;; | ||
| ;; - (logger-timestamp) returns True iff messages are prefixed with | ||
| ;; date and time. Default: True. | ||
| ;; | ||
| ;; - (logger-set-filepath FILEPATH) sets the destination of the | ||
| ;; filepath to FILEPATH, where log messages are written. | ||
| ;; | ||
| ;; - (logger-set-stdout FLAG) sets the flag to write log messages to | ||
| ;; stdout iff FLAG is True. | ||
| ;; | ||
| ;; - (logger-set-timestamp FLAG) sets the flag to prefix log messages | ||
| ;; with date and time iff FLAG is True. | ||
| ;; | ||
| ;; - (logger-level) returns the current log level. Only messages are | ||
| ;; at this level or higher are logged. For instance, if the current | ||
| ;; log level is info, then messages at levels info, warn, error and | ||
| ;; fatal will be logged. Default: info. | ||
| ;; | ||
| ;; - (logger-levels) returns a tuple with all possible log levels, | ||
| ;; which are trace, debug, info, warn, error and fatal. | ||
| ;; | ||
| ;; - (logger-trace MESSAGE) log MESSAGE at the trace level. | ||
| ;; | ||
| ;; - (logger-debug MESSAGE) log MESSAGE at the debug level. | ||
| ;; | ||
| ;; - (logger-info MESSAGE) log MESSAGE at the info level. | ||
| ;; | ||
| ;; - (logger-warn MESSAGE) log MESSAGE at the warn level. | ||
| ;; | ||
| ;; - (logger-error MESSAGE) log MESSAGE at the error level. | ||
| ;; | ||
| ;; - (logger-fatal MESSAGE) log MESSAGE at the fatal level. Note that | ||
| ;; this will not halt the process. This responsability is left to | ||
| ;; the programmer. | ||
| ;; | ||
| ;; - (logger-log LEVEL MESSAGE) log MESSAGE at level LEVEL. | ||
| ;; | ||
| ;; - (logger-logf LEVEL MESSAGE ARGUMENTS) log MESSAGE at level LEVEL, | ||
| ;; formatting the message with ARGUMENTS according | ||
| ;; https://www.swi-prolog.org/pldoc/man?predicate=format/3. | ||
| ;; | ||
| ;; - (logger-tracef MESSAGE ARGUMENTS) like logger-trace but expects a | ||
| ;; formatted message. | ||
| ;; | ||
| ;; - (logger-debugf MESSAGE ARGUMENTS) like logger-debug but expects a | ||
| ;; formatted message. | ||
| ;; | ||
| ;; - (logger-infof MESSAGE ARGUMENTS) like logger-info but expects a | ||
| ;; formatted message. | ||
| ;; | ||
| ;; - (logger-warnf MESSAGE ARGUMENTS) like logger-warn but expects a | ||
| ;; formatted message. | ||
| ;; | ||
| ;; - (logger-errorf MESSAGE ARGUMENTS) like logger-error but expects a | ||
| ;; formatted message. | ||
| ;; | ||
| ;; - (logger-fatalf MESSAGE ARGUMENTS) like logger-fatal but expects a | ||
| ;; formatted message. | ||
|
|
||
| ;; Import prolog logger library | ||
| !(import! &self (library lib_import)) | ||
| !(import_prolog_functions_from_file (library lib_logger.pl) | ||
| (logger-filepath | ||
| logger-stdout | ||
| logger-timestamp | ||
| logger-set-filepath | ||
| logger-set-stdout | ||
| logger-set-timestamp | ||
| logger-level | ||
| logger-levels | ||
| logger-set-global-level | ||
| logger-set-local-level | ||
| logger-set-level | ||
| logger-trace | ||
| logger-debug | ||
| logger-info | ||
| logger-warn | ||
| logger-error | ||
| logger-fatal | ||
| logger-log | ||
| logger-logf | ||
| logger-tracef | ||
| logger-debugf | ||
| logger-infof | ||
| logger-warnf | ||
| logger-errorf | ||
| logger-fatalf)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I think it should go to a separate repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that it potentially requires an installation step from the user
makes me want to agree. I suppose, only if this installation is handled automatically or added to build.sh or such, can it be part of the standard library, otherwise it is probably better hosted on its own repo.
I am neutral on this. So just provide some final confirmation and I will move it to its own repo.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can upload your library to a repository
petta_lib_logger(name convention for PeTTa libs), the way it works:lib_logger.pl,lib_logger.mettaandlogger_example.mettaare in the root of the repository. Then you can import your library with the log4p installation step included: !(git-import! "https://github.com/ngeiswei/petta_lib_logger" "swipl pack install log4p")Last: please also add it to the registry page: https://github.com/trueagi-io/PeTTa/wiki/Extension-libraries
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have moved it to its own repo, however it fails to pass the tests in logger_example.metta. Any idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Yes: ngeiswei/petta_lib_logger#1
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @patham9. I've added petta_lib_logger to Extension-libraries, note that I took the freedom to reformat the page a bit (use markdown lists + added some more hyper-links). Let me know if you wish the formatting to be different.