jjwiseman
2019-5-9 17:30:20

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?


samth
2019-5-9 17:35:15

@jjwiseman I also don’t know of anything that does that


greg
2019-5-9 19:13:16

@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?


notjack
2019-5-9 19:24:20

tangential opinion: all racket log messages should include a module name (e.g. “racket/contract: something something chaperones something unsafe”)


d_run
2019-5-9 19:30:16

greg
2019-5-9 19:45:24

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”?


jjwiseman
2019-5-9 19:46:41

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


greg
2019-5-9 19:46:51

Anyway, quick glance at that screen shot, it looks like a Racket log-receiver could do this.


greg
2019-5-9 19:47:08

Like, the one for racket-logger-mode in Emacs does.


greg
2019-5-9 19:47:43

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.


greg
2019-5-9 19:48:26

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.


greg
2019-5-9 19:49:23

(Or neither, maybe it’s fine to define a few ANSI color escape codes locally for this. idk.)


jjwiseman
2019-5-9 19:49:32

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.


greg
2019-5-9 19:49:58

Have you seen define-logger in Racket?


jjwiseman
2019-5-9 19:50:38

oh, that looks about perfect. i missed that and had been using make-logger


greg
2019-5-9 19:51:07

Yep. :slightly_smiling_face:


jjwiseman
2019-5-9 19:51:40

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


greg
2019-5-9 19:52:20

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.


greg
2019-5-9 19:53:36

Oh, I didn’t know about ansi-color.


greg
2019-5-9 19:53:58

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.


jjwiseman
2019-5-9 19:54:04

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.


greg
2019-5-9 19:54:21

Yes.


greg
2019-5-9 19:54:59

There’s a default log-receiver that prints to stdout or stderr, depending on the PLTSTDERR and PLTSTDERR env vars.


greg
2019-5-9 19:55:27

You can define as many other log-receivers as you need/want.


greg
2019-5-9 19:55:36

Each can have its own topics/levels it cares about.


jjwiseman
2019-5-9 19:56:54

(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)


greg
2019-5-9 19:57:34

Yes. Topics were added later.


d_run
2019-5-9 20:13:02

funny i just wrote something like define/provide-logger for a json logger


jjwiseman
2019-5-9 20:30:53

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.


notjack
2019-5-9 20:57:42

the choice of module name doesn’t have to reflect where the log message happens IMO


notjack
2019-5-9 20:58:46

I think it’s reasonable for a logger defined in foo/bar/private/utility to log messages with foo/bar


notjack
2019-5-9 21:00:20

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.