Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions examples/logger.metta
Copy link
Collaborator

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.

Copy link
Contributor Author

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

swipl pack install log4p

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.

Copy link
Collaborator

@patham9 patham9 Feb 11, 2026

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.metta and logger_example.metta are 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

Copy link
Contributor Author

@ngeiswei ngeiswei Feb 12, 2026

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@ngeiswei ngeiswei Feb 16, 2026

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.

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)
107 changes: 107 additions & 0 deletions lib/lib_logger.metta
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))
Loading