Hi,
I am quite new to lisp, so hopefully my question is not too "dumb". ;-)
I'm curious if it is possible to dynamically set the log level when creating a sender like:
(defun init-logging (log-level) (log5:defoutput human-time (human-time)) (log5:start-sender *my-logger* (log5:stream-sender :location *standard-output*) :category-spec log-level :output-spec (log5:category human-time log5:message log5:context)))
I know, that it does not work like that, but is there another way than using configure-from-file?
Thanks in advanced for your help.
Stefan
Hi Stefan,
I'm curious if it is possible to dynamically set the log level when creating a sender like:
(defun init-logging (log-level) (log5:defoutput human-time (human-time)) (log5:start-sender *my-logger* (log5:stream-sender :location *standard-output*) :category-spec log-level :output-spec (log5:category human-time log5:message log5:context)))
Other than moving the defoutput from inside the defun to outside it (for efficiency reasons), I think that what you have there is exactly right.
I know, that it does not work like that, but is there another way than using configure-from-file?
Both the start-sender macro and configure-from-file call the start- sender-fn so your definition ought to work. What happens when you try it?
Thanks in advanced for your help.
My pleasure. -- Gary Warren King, metabang.com Cell: (413) 559 8738 Fax: (206) 338-4052 gwkkwg on Skype * garethsan on AIM
Hi Gary,
Gary King wrote:
Hi Stefan,
I'm curious if it is possible to dynamically set the log level when creating a sender like:
(defun init-logging (log-level) (log5:defoutput human-time (human-time)) (log5:start-sender *my-logger* (log5:stream-sender :location *standard-output*) :category-spec log-level :output-spec (log5:category human-time log5:message log5:context)))
Other than moving the defoutput from inside the defun to outside it (for efficiency reasons), I think that what you have there is exactly right.
Since I init the logger only once during the program run it is fine ;-)
I know, that it does not work like that, but is there another way than using configure-from-file?
Both the start-sender macro and configure-from-file call the start-sender-fn so your definition ought to work. What happens when you try it?
OK, my fault. I could have been more verbose:
I tried it in several ways:
(defun init-logging (log-level)
(log5:defoutput human-time (human-time)) (log5:start-sender *my-logger* (log5:stream-sender :location *standard-output*) :category-spec (translate-loglevel-from-string log-level) :output-spec (log5:category human-time log5:message log5:context))))
(defun translate-loglevel-from-string (log-level) (cond ((string= "error") (log5:error+)) ..
It that case it does not work because sbcl can't find the function "error+". If I don't use the paranthesis, the stream-sender raises an error complaining that translate-loglevel-from-string is not a known category.
To sum up, I have a program, which gets the log-level as a string. I want to translate this string to a log5 log level and pass it to the creation of a logger. I hope I could give you an image, of what I would need.
Thanks in advanced for your help.
My pleasure.
Once again, I really appreciate your help.
Stefan
Hi Stefan,
Your code was close but not quite there. Syntax can really get in the way of things. <smile>
Here is a slightly modified version of your code with some extra comments:
I define this silly human-time so that things work...
(defun human-time () (get-universal-time))
In the start-sender macro, the category-spec and the output-spec both get evaluated. This means that the macro doesn't quote them for you so that you can use functions (which is good, that's what you want to do!). The only change I've made here is to quote the list (log5:category human-time log5:message log5:context).
(defun init-logging (log-level) (log5:defoutput human-time (human-time)) (log5:start-sender 'foo (log5:stream-sender :location *standard-output*) :category-spec (translate-loglevel-from-string log-level) :output-spec '(log5:category human-time log5:message log5:context)))
Similarly, the return value from the cond expression (log5:error+) needs to be quoted (as it is below). If you don't quote the expression, then Lisp will evaluate it. When Lisp evaluates (log5:error+) it tries to see if the first expression in the list -- i.e., log5:error -- is a function. Since it's not a function, Lisp complains. When I quote the list, then Lisp knows to just return it.
(defun translate-loglevel-from-string (log-level) (cond ((string= log-level "error") '(log5:error+)) (t (error "cannot translate log level: ~s" log-level))))
Let me know if anything is still unclear or if something doesn't work the way you expect.
-- Gary Warren King, metabang.com Cell: (413) 559 8738 Fax: (206) 338-4052 gwkkwg on Skype * garethsan on AIM