question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace newline `\n` with literal `\\n` in log

See original GitHub issue

I’d love the option to force log messages to span just one line by replacing \n with \\n.

This is an issue which are sometimes encountered when logging Pandas DataFrames and Numpy Arrays.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, Mar 11, 2020

Yep, that’s totally possible. Instead of modifying record["message"], just create a new entry to be used by the format parameter of some specific sinks only.

def flatten_message(record):
    record["extra"]["flattened_message"] = record["message"].replace("\n", "\\n")

logger.add("file.log", format="[{time}] {extra[flattened_message]}")
logger = logger.patch(flatten_message)
1reaction
Delgancommented, Jan 11, 2020

Oh, yeah, sorry, I completely misinterpreted your question.

So, here is another workaround I can offer you. 😄

>>> from loguru import logger
>>> multi = "foo\nbar\nbaz\n"
>>> logger.info(multi)
2020-01-11 17:32:33.496 | INFO - __main__ - foo
bar
baz

>>> def flatten_message(record):
...     record["message"] = record["message"].replace("\n", "\\n")
... 
>>> logger = logger.patch(flatten_message)
>>> logger.info(multi)
2020-01-11 17:33:16.592 | INFO - __main__ - foo\nbar\nbaz\n

I think you can use patch() to force the message to span only one line. You can also apply the change for your whole application by using the patcher parameter of the configure() method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replace newlines with literal \n - Stack Overflow
This works, but not for special characters like \r, \n, etc. What I'm trying to do is to replace the newline character by...
Read more >
sed - How can I replace a newline with its escape sequence?
varName - Name of the variable containing the content; // - Replace all instances of... $'\n' - The literal newline character (we're turning...
Read more >
Vim - How to replace a newline with the string "\n"
start an ex command 1,$-1 over a range from the first line till the last but one s/ substitute \n all newlines /...
Read more >
JavaScript Program to Replace All Line Breaks with <br>
The RegEx is used with the replace() method to replace all the line breaks in string with <br>. The pattern /(\r\n|\r|\n)/ checks for...
Read more >
Find and replace can no longer replace with newline ('\n') #5657
Open find and replace (CTRL+H) · Search for any text (e.g. " ") · Set replacement text to include '\n' (e.g. "\n") image...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found