katzgrau / KLogger

A Simple Logging Class For PHP
http://codefury.net/projects/klogger/
981 stars 284 forks source link

Option to tabulate log lines #64

Closed webbird closed 8 years ago

webbird commented 9 years ago

I thought my log file would be more readable if all messages are indented with the same depth, no matter what $level. Example:

original

[2015-08-31 19:02:49.181617] [info] dumdidum
[2015-08-31 19:02:49.182616] [emergency] mist

nicer

[2015-08-31 19:03:27.268881] [info]      dumdidum
[2015-08-31 19:03:27.268881] [emergency] mist

So I changed line 553 (method formatMessage()) like this (original line is the commented one):

        //$message = "[{$this->getTimestamp()}] [{$level}] {$message}";
        $message = "[{$this->getTimestamp()}] [{$level}] ".str_repeat(' ',(9-strlen($level)))."{$message}";

Maybe it's helpful for someone.

tleb commented 9 years ago

Seems like a nice feature, why not a PR?

katzgrau commented 9 years ago

Really? Maybe if it was a configurable option. I'm really feeling like this is a nice to have that may end up requiring more changes than expected.

But in the end, not something that would make or break someone's decision to use the library.

@onno-vos-dev @richjenks ?

onno-vos-dev commented 9 years ago

(My personal opinion)

Personally, I'm not a fan of this sort of indenting. I think the log message gets separated too much from the loglevel and makes your eyes scan back and forth between the loglevel and the logmessage. I prefer my code and my logfiles to be as clean as possible with only the absolute necessary amount of whitespace. This fix clutters the logfile with unnecessary whitespace if you ask me.

@webbird If you really need this, you can TRY to use the custom logformat that PR https://github.com/katzgrau/KLogger/pull/54 has introduced. I just gave it 5 minutes and gave up because I couldn't get it to work but perhaps you can give it another go.

But again, in my opinion, you shouldn't be formatting your logfile in such a way to start with.

Can you elaborate on the use case for this?

Because if you're ending up reading your logfiles so frequently that you end up adjusting the formatting of your logfiles, I'd suggest you look at things like Logstash, Splunk or other tools that can help you plow through logfiles and make them visual and easily human readable.

webbird commented 9 years ago

Please, this was just for people who may need it too. I didn't want to start a lengthy discussion about pros and cons, likes and dislikes, or something else. It's something I found that it makes the logs more readable for me, not a feature request or anything. Just close this and that's it. Sorry for this.

richjenks commented 9 years ago

I agree with @katzgrau and @onno-vos-dev

Perhaps this would be better as an example format param in the readme rather than a separate feature?

webbird commented 9 years ago

I don't think you can make calculations in the format param?

richjenks commented 9 years ago

You can do what you like with the format param, as long as it returns a string.

katzgrau commented 9 years ago

I think it would have to be a special format param, like [level-padded]

richjenks commented 9 years ago

Good idea, I'd support that.

katzgrau commented 9 years ago

What does everyone think of https://github.com/katzgrau/KLogger/commit/cbd026ded7ae21243badae7979bdfbfb6d2ce877

katzgrau commented 9 years ago

The reason I did it that way (ie, just whitespace) is that it gives the developer a bit of flexibility in how they arrange the log format.

richjenks commented 9 years ago

I like it, does the job and is nice and simple.

onno-vos-dev commented 9 years ago

@katzgrau I'm fine with this but will make a note that if you have a log-message which stretches multiple lines, you're still gonna have a messy log-file. Ideally you'd want to also pad the entire message to the same position so if a new-line is reached, that should be padded by the number of characters which are in "[{date}] [{level}]"

[2015-08-31 19:03:27.268881] [info]      Short ribs fatback swine pastrami shankle, jowl beef.
[2015-08-31 19:03:27.268881] [emergency] Bacon ipsum dolor amet ball tip venison chuck landjaeger, tail frankfurter shankle pork short loin turducken pork chop tri-tip.
Ham hock tail turkey venison swine, cow tenderloin pastrami pork bacon tri-tip.

I'm fine with the commit you just made @katzgrau just giving my $0,02 that this will note solve larger logfiles. Which tends to happen when you add $context or append JSON-blobs to the message.

richjenks commented 9 years ago

I see what you mean, but I don't think it's practical because there may be formats that don't begin with date and level. For example, if the format is "[{date}] [Project name] [{level}]" then it would be pretty tricky to indent subsequent lines.

onno-vos-dev commented 9 years ago

@richjenks I know and I'm just adding my $0,02 that this won't be the magic bullet for the OCD-minded users of KLogger ;-)

richjenks commented 9 years ago

@onno-vos-dev fair enough. If such people are that bothered, maybe they should just get wider monitors :)

onno-vos-dev commented 9 years ago

@richjenks Agreed :+1: Either way, when you start having very verbose output, you're gonna get stuck with logfiles which may be hard to read by a human but they can be very useful for alarms, compliance or debugging reasons which is mainly why where I work, we produce very verbose logs.

katzgrau commented 8 years ago

Officially in the latest release (1.2.0)