[GH-ISSUE #537] BrowserConsoleHandler doesn't quote backslashes #188

Closed
opened 2026-03-04 02:13:00 +03:00 by kerem · 0 comments
Owner

Originally created by @torohill on GitHub (Apr 7, 2015).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/537

I'm logging SQL statements to the browser console and was finding that backslashes weren't being quoted correctly in the generated Javascript.

For example, I was trying to log something like this:

INSERT INTO foo (bar) VALUES ("<a href=\"http://www.example.com/\">example.com</a>")

And the Javascript generated looked like this:

<script>(function (c) {if (c && c.groupCollapsed) {
c.log("%c%csql%c %cINFO%c INSERT INTO foo (bar) VALUES (\"<a href=\\"http://www.example.com/\\">example.com</a>\")", "font-weight: normal", "font-weight: bold", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
}})(console);</script>

This causes a syntax error because the sequence href=\\"http is not quoted correctly.

I found that making the following change to BrowserConsoleHandler.php fixed the problem in my particular case:

diff -r 959278783984 vendor/monolog/monolog/src/Monolog/Handler/BrowserConsoleHandler.php
--- a/vendor/monolog/monolog/src/Monolog/Handler/BrowserConsoleHandler.php      Tue Apr 07 11:36:02 2015 +1200
+++ b/vendor/monolog/monolog/src/Monolog/Handler/BrowserConsoleHandler.php      Tue Apr 07 12:33:54 2015 +1200
@@ -166,7 +166,7 @@

     private static function quote($arg)
     {
-        return '"' . addcslashes($arg, "\"\n") . '"';
+        return '"' . addcslashes($arg, "\"\n\\") . '"';
     }

     private static function call()

After this change the generated Javascript looked like this:

<script>(function (c) {if (c && c.groupCollapsed) {
c.log("%c%csql%c %cINFO%c INSERT INTO foo (bar) VALUES (\"<a href=\\\"http://www.example.com/\\\">example.com</a>\")", "font-weight: normal", "font-weight: bold", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
}})(console);</script>
Originally created by @torohill on GitHub (Apr 7, 2015). Original GitHub issue: https://github.com/Seldaek/monolog/issues/537 I'm logging SQL statements to the browser console and was finding that backslashes weren't being quoted correctly in the generated Javascript. For example, I was trying to log something like this: ``` sql INSERT INTO foo (bar) VALUES ("<a href=\"http://www.example.com/\">example.com</a>") ``` And the Javascript generated looked like this: ``` javascript <script>(function (c) {if (c && c.groupCollapsed) { c.log("%c%csql%c %cINFO%c INSERT INTO foo (bar) VALUES (\"<a href=\\"http://www.example.com/\\">example.com</a>\")", "font-weight: normal", "font-weight: bold", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal"); }})(console);</script> ``` This causes a syntax error because the sequence `href=\\"http` is not quoted correctly. I found that making the following change to `BrowserConsoleHandler.php` fixed the problem in my particular case: ``` diff diff -r 959278783984 vendor/monolog/monolog/src/Monolog/Handler/BrowserConsoleHandler.php --- a/vendor/monolog/monolog/src/Monolog/Handler/BrowserConsoleHandler.php Tue Apr 07 11:36:02 2015 +1200 +++ b/vendor/monolog/monolog/src/Monolog/Handler/BrowserConsoleHandler.php Tue Apr 07 12:33:54 2015 +1200 @@ -166,7 +166,7 @@ private static function quote($arg) { - return '"' . addcslashes($arg, "\"\n") . '"'; + return '"' . addcslashes($arg, "\"\n\\") . '"'; } private static function call() ``` After this change the generated Javascript looked like this: ``` javascript <script>(function (c) {if (c && c.groupCollapsed) { c.log("%c%csql%c %cINFO%c INSERT INTO foo (bar) VALUES (\"<a href=\\\"http://www.example.com/\\\">example.com</a>\")", "font-weight: normal", "font-weight: bold", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal"); }})(console);</script> ```
kerem closed this issue 2026-03-04 02:13:00 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#188
No description provided.