Last active
June 12, 2025 11:44
-
-
Save joeytwiddle/e7cccc1e3adc83512220078ec62d61e7 to your computer and use it in GitHub Desktop.
Revisions
-
joeytwiddle revised this gist
Jun 12, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ Fastify logging works differently from `console.log()`. It has a few gotchas. - If you want to **log an error**, it must be the first argument, and _not_ a property inside an object. You may add your own string message as the second argument (optional but recommended). - If you want to **log an object**, then put the object as the first argument, and again an optional message string as the second argument. - But beware: If the "object" you are logging is **actually a string**, then _do not_ put it as the first variable. Either wrap it inside an object, or concatenate it into your message. ## Patterns which **WORK** -
joeytwiddle revised this gist
May 29, 2025 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,6 +39,9 @@ fastify.log.info(stringVariable, "message") // An error deep inside an object does not get serialized properly. // This will only log: msg: "test_log", error: {} fastify.log.info({ error }, "message") // Unfortunately this means we cannot log both an error and some contextual data at the same time // My solution to this is to send two logs, one with data, and one with the error // Please correct me if there is a better way! ``` ## And one more surprising case: -
joeytwiddle revised this gist
May 27, 2025 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,8 +4,8 @@ Fastify logging works differently from `console.log()`. It has a few gotchas. ## TLDR - If you want to **log an error**, it must be the first argument, and _not_ a property inside an object. You may add your own string message as the second argument (optional but recommended). - If you want to **log an object**, then put the object as the first argument, and again an optional message string as the second argument. - But beware: If the "object" you are logging is **actually a string**, then _do not_ put it as the first variable, concatenate it instead. ## Patterns which **WORK** @@ -25,19 +25,19 @@ fastify.log.debug({ foo: 'bar' }, { baz: 'qux' }) // Through pino, this will appear as: {baz: "qux"}, foo: "bar" ``` ## Patterns which surprisingly **DO NOT WORK** ```javascript // Providing a string as the first argument? Only the first argument gets logged! // This will not log the data fastify.log.info("message", { data }) // This will not log the variable fastify.log.info("message", stringVariable) // This will not log the message fastify.log.info(stringVariable, "message") // An error deep inside an object does not get serialized properly. // This will only log: msg: "test_log", error: {} fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Apr 30, 2025 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,9 +39,13 @@ fastify.log.info(stringVariable, "message") // An error deep inside an object does not get serialized. // Only logs: msg: "test_log", error: {} fastify.log.info({ error }, "message") ``` ## And one more surprising case: ```javascript // When logging a string property, it does not appear if I use the key 'msg', but it does with key 'message' // I'm not sure if this is always true for Fastify. It might just be in my current project. fastify.log.info({ val, msg }, "only val will be logged") fastify.log.info({ val, message }, "both val and message will be logged") ``` -
joeytwiddle revised this gist
Apr 30, 2025 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,4 +39,9 @@ fastify.log.info(stringVariable, "message") // An error deep inside an object does not get serialized. // Only logs: msg: "test_log", error: {} fastify.log.info({ error }, "message") // Logging a msg property does not appear, but logging a message property does! // I'm not sure if this is always true for Fastify. It might just be in my current project. fastify.log.info({ val, msg }, "only val will be logged") fastify.log.info({ val, message }, "both val and message will be logged") ``` -
joeytwiddle revised this gist
Mar 22, 2025 . 1 changed file with 14 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,15 @@ # Logging in Fastify Fastify logging works differently from `console.log()`. It has a few gotchas. ## TLDR - If you want to **log an object**, then put the object as the first argument, and (optionally) a message string as the second argument. - If you want to **log an error object**, it must be the first argument, and not a property inside an object. - But beware: If the "object" you are logging is **actually a string**, then _do not_ put it as the first variable, concatenate it instead. ## Patterns which **WORK** ```javascript // Logging one string fastify.log.info("message") @@ -16,11 +21,12 @@ fastify.log.info(error, "message") fastify.log.info({ data, foo, bar, baz }, "message") // You can also log two objects, although the second will be treated as the msg fastify.log.debug({ foo: 'bar' }, { baz: 'qux' }) // Through pino, this will appear as: {baz: "qux"}, foo: "bar" ``` ## Patterns which unexpectedly **DO NOT WORK** ```javascript // Providing a string as the first argument? Only the first argument gets logged! // Does not log the data -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,12 +6,18 @@ TLDR: Patterns which are **WORKING**: ```javascript // Logging one string fastify.log.info("message") fastify.log.info("message: " + stringVariable) fastify.log.info(`message: ${stringVariable}`) // Puttting the error or context object first, and the log message second fastify.log.info(error, "message") fastify.log.info({ data, foo, bar, baz }, "message") // You can also log two objects, although the second will be treated as the msg // This logs: msg: {baz: "qux"}, foo: "bar" fastify.log.debug({ foo: 'bar' }, { baz: 'qux' }) ``` Patterns which unexpectedly **DO NOT WORK**: @@ -25,6 +31,6 @@ fastify.log.info("message", stringVariable) fastify.log.info(stringVariable, "message") // An error deep inside an object does not get serialized. // Only logs: msg: "test_log", error: {} fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ fastify.log.info("message", stringVariable) // Does not log the message fastify.log.info(stringVariable, "message") // An error deep inside an object does not get serialized. // Only logs: "msg": "test_log", "error": {} fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ fastify.log.info("message: " + stringVariable) fastify.log.info(`message: ${stringVariable}`) fastify.log.info(error, "message") fastify.log.info({ data, foo, bar, baz }, "message") ``` Patterns which unexpectedly **DO NOT WORK**: -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,16 +14,17 @@ fastify.log.info(error, "message") fastify.log.info({ data }, "message") ``` Patterns which unexpectedly **DO NOT WORK**: ```javascript // Providing a string as the first argument? Only the first argument gets logged! // Does not log the data fastify.log.info("message", { data }) // Does not log the variable fastify.log.info("message", stringVariable) // Does not log the message fastify.log.info(stringVariable, "message") // An error deep inside an object does not get serialised. // Only logs: "msg": "test_log", "error": {} fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,13 +16,14 @@ fastify.log.info({ data }, "message") Patterns which **DO NOT WORK**: ```javascript // String as the first argument? Only the first argument gets logged! // Only logs the message, ignores the data object! fastify.log.info("message", { data }) // Does not log the variable fastify.log.info("message", stringVariable) // Does not log the message fastify.log.info(stringVariable, "message") // Error deep inside an object does not get serialised. It only logs: "msg":"test_log","error":{} fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,6 @@ fastify.log.info(stringVariable, "message") // Object as the second argument? Only logs the message, ignores the data object! fastify.log.info("message", { data }) // Error deep inside an object does not get serialised. It only logs: "msg":"test_log","error":{} fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,6 @@ fastify.log.info(stringVariable, "message") // Object as the second argument? Only logs the message, ignores the data object! fastify.log.info("message", { data }) // Error deep inside an object? Only logs: message { error: {} } fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,13 +16,13 @@ fastify.log.info({ data }, "message") Patterns which **DO NOT WORK**: ```javascript // Two string arguments? Only the first argument get logged! fastify.log.info("message", stringVariable) fastify.log.info(stringVariable, "message") // Object as the second argument? Only logs the message, ignores the data object! fastify.log.info("message", { data }) // Error deep inside an object? Only logs: { error: "[object Error]" } fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,16 +2,24 @@ TLDR: - If you want to log an object, put the object as the first argument, and (optionally) a message string as the second argument. - If you want to log an error object, it must be the first argument, and not a property inside an object. - (As a result, I don't know how to log an error and also an object for context, but it might be possible.) - But beware: If the "object" you are logging is actually a string, then _do not_ put it as the first variable, concatenate it instead. Patterns which are **WORKING**: ```javascript fastify.log.info("message") fastify.log.info("message: " + stringVariable) fastify.log.info(`message: ${stringVariable}`) fastify.log.info(error, "message") fastify.log.info({ data }, "message") ``` Patterns which **DO NOT WORK**: ```javascript // Only the first arguments get logged! fastify.log.info("message", stringVariable) fastify.log.info(stringVariable, "message") // Only logs the message, ignores the data object! fastify.log.info("message", { data }) -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,14 +3,14 @@ TLDR: - If you want to log an error object, it must be the first argument, and not a property inside an object. - (As a result, I don't know how to log an error and also an object for context, but it might be possible.) Patterns which are **WORKING**: ```javascript fastify.log.info("message") fastify.log.info(error, "message") fastify.log.info({ data }, "message") ``` Patterns which **DO NOT WORK**: ```javascript // Only logs the message, ignores the data object! fastify.log.info("message", { data }) -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ TLDR: - If you want to log an object, put the object as the first argument, and (optionally) a message string as the second argument. - If you want to log an error object, it must be the first argument, and not a property inside an object. - (As a result, I don't know how to log an error and also an object for context, but it might be possible.) Working patterns: ```javascript -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ TLDR: - If you want to log an object, put the object as the first argument, and (optionally) a message string as the second argument. - If you want to log an error, it must be the first argument, and not deep inside an object. - (I don't know how to log an error and also an object for context, but it might be possible) -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,9 +12,9 @@ fastify.log.info({ data }, "message") Patterns which don't work: ```javascript // Only logs the message, ignores the data object! fastify.log.info("message", { data }) // Only logs: { error: "[object Error]" } fastify.log.info({ error }, "message") ``` -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ TLDR: - If you want to log an object, put the object as the first argument, and (optionally) the message string as the second argument! - If you want to log an error, it must be the first argument, and not deep inside an object. - (I don't know how to log an error and also an object for context, but it might be possible) -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ fastify.log.info({ data }, "message") Patterns which don't work: ```javascript // Only logs: { error: "[object Error]" } fastify.log.info({ error }, "message") // Only logs the message! -
joeytwiddle revised this gist
Mar 4, 2025 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,14 +4,14 @@ TLDR: - (I don't know how to log an error and also an object for context, but it might be possible) Working patterns: ```javascript fastify.log.info("message") fastify.log.info(error, "message") fastify.log.info({ data }, "message") ``` Patterns which don't work: ```javascript // Only logs: `error: "[object Error]"` fastify.log.info({ error }, "message") -
joeytwiddle renamed this gist
Mar 4, 2025 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
joeytwiddle created this gist
Mar 4, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ TLDR: - If you want to log an object, put the object as the first argument, and the message string as the second argument! - If you want to log an error, it must be the first argument, and not deep inside an object. - (I don't know how to log an error and also an object for context, but it might be possible) Working patterns: ``` fastify.log.info("message") fastify.log.info(error, "message") fastify.log.info({ data }, "message") ``` Patterns which don't work: ``` // Only logs: `error: "[object Error]"` fastify.log.info({ error }, "message") // Only logs the message! fastify.log.info("message", { data }) ```