Jump to content

How to log horizontal tabs?


LAPS
 Share

Recommended Posts

Hello there, 

I'm trying to log some text that includes horizontal tabs (\t), this way:

wire('log')->save("log-file", "{$data['one']}\t{$data['two']}\t{$data['three']}")

However, when I download and open the log file then tabs are not included i.e. tabs changed to white spaces. Maybe, it happens because of this.

 

How should I write tabs to log files?

Link to comment
Share on other sites

Currently this is not possible because the ProcessWire logs save additional information for each message you save to a log (timestamp, current user et c.). Since the logs are stored as plaintext files, this information is stored as tab-delimited lines. This can only work if all linebreaks and tabs are stripped from each message, which is exactly what WireLog does. Interestingly enough, there is a "delimiter" option to change the delimiter used to seperate those bits of information. But it doesn't change the fact that all tabs are stripped out from the message. Maybe that would warrant a feature request? That is, if a custom delimiter is passed strip that from the message itself and keep tabs in. Though I'm not sure how much work that would be.

Why do you need tabs inside log messages anyway? If you want to display the log messages in a structured format, maybe using regular pages and fields would be better suited. Or just use something else as a delimiter, like a pipe |. You could even str_replace the pipes back to tabs in your output ...

Link to comment
Share on other sites

@MoritzLost thanks for the explanation, and I would like to know why tabs are stripped out from the log text while they are not stripped between the "standard" PW-added data such as 'showUser', 'showURL', 'user', and 'url'.

10 minutes ago, MoritzLost said:

Why do you need tabs inside log messages anyway? If you want to display the log messages in a structured format, maybe using regular pages and fields would be better suited. Or just use something else as a delimiter, like a pipe |. You could even str_replace the pipes back to tabs in your output ...

It is because I would like to use offline the log data that is in a structured format.

Link to comment
Share on other sites

16 minutes ago, LAPS said:

thanks for the explanation, and I would like to know why tabs are stripped out from the log text while they are not stripped between the "standard" PW-added data such as 'showUser', 'showURL', 'user', and 'url'.

@LAPS Because it's plaintext, and ProcessWire needs to be able to parse log lines back into structured information (user, url, timestamp, message) for $log->getEntries() to work. Take a look at the source of lineToEntry: This method splits a log line on tabs and returns an associative array. Now if the "message" part of the line could include an arbitrary amount of tabs, the method couldn't really do that, because you wouldn't know how many parts the explode method would yield for that line. You could make the argument that it only needs to parse the first few array elements and join the rest with tabs again, but since the log may or may not contain the URL and the user, you don't know exactly how many array entries are bits of meta data, and which belong to the message.

I'm not saying it's perfect design by the way (though I do think it's an elegant solution despite this edge-case), just trying to explain why I think it was build this way ?

16 minutes ago, LAPS said:

It is because I would like to use offline the log data that is in a structured format.

In this case I'd go with the approach I mentioned above, using a different delimiter and optionally changing it back to a tab in your output.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...