i’m looking for code to support colorized logging, but haven’t been able to find any. anyone know of any existing code for that?
@jjwiseman I also don’t know of anything that does that
@jjwiseman I don’t either but I’m curious. Do you mean ANSI color codes for a terminal? Are you thinking different colors for log levels and/or topics (so this could be done by a log receiver), or, fancy color formatting within each logger message?
tangential opinion: all racket log messages should include a module name (e.g. “racket/contract: something something chaperones something unsafe”)
In case someone is thinking of making a Racket package like this: As a U.S. person I’d suggest a slightly less-uncomfortable name, like “colorful-logs” or “rainbow-logs”?
right, ANSI color codes for terminal, and my desire is for different colors for each topic, though people often do different colors for each level
Anyway, quick glance at that screen shot, it looks like a Racket log-receiver could do this.
Like, the one for racket-logger-mode
in Emacs does.
So call sites don’t need to know or care, and, this needn’t affect log-receivers outputting to files or to JSON on CloudWatch or whatever else.
Is raart
the preferred way to do ANSI codes in Racket these days, or charterm
? I’m not sure. Whatever it is, that plus a log-receiver would be the way to go, I think.
(Or neither, maybe it’s fine to define a few ANSI color escape codes locally for this. idk.)
yes, that part seems straightforward. i’m thinking about some convenience aspects. like, in clojurescript i’ve done something like a deflogger
macro that defines a logger and a log-info
function that uses that logger. just to avoid the (log-message logger 'info ...)
etc for each call to logging.
Have you seen define-logger
in Racket?
oh, that looks about perfect. i missed that and had been using make-logger
Yep. :slightly_smiling_face:
and the top half of the google results page i was looking at was all about https://docs.racket-lang.org/ansi-color/index.html
It defines 4 functions for you, log-<topic>-<level>
. One tip, if you want to use those outside the module where you have define-logger
, you will need to provide all 4 by name. IOW there’s no define/provide-logger
macro.
Oh, I didn’t know about ansi-color
.
There are enough Racket projects these days, it’s hard to keep up. Which is a good problem to have, compared to a few years ago.
so it looks like i can make a receiver for a parent logger, and then i can have multiple define-loggers
that use that parent without having to make separate receivers for each child.
Yes.
There’s a default log-receiver that prints to stdout or stderr, depending on the PLTSTDERR and PLTSTDERR env vars.
You can define as many other log-receivers as you need/want.
Each can have its own topics/levels it cares about.
(it is slightly annoying that unless callers take special care, by the time the receiver gets a log message it already has the "<topic>:"
prefix)
Yes. Topics were added later.
funny i just wrote something like define/provide-logger
for a json logger
i kind of like topics because they aren’t tied to a specific module, but i’ve definitely been in the situation where i wish i knew where to start looking for which damn file was logging a particular message.
the choice of module name doesn’t have to reflect where the log message happens IMO
I think it’s reasonable for a logger defined in foo/bar/private/utility
to log messages with foo/bar
If I started seeing log messages prefixed with foo/bar
I would go to the scribble docs defining the foo/bar
module and look for any text mentioning log messages. That kind of workflow is what I want log message best practices to encourage.