Skip to content

Instantly share code, notes, and snippets.

@joeytwiddle
Last active June 12, 2025 11:44
Show Gist options
  • Select an option

  • Save joeytwiddle/e7cccc1e3adc83512220078ec62d61e7 to your computer and use it in GitHub Desktop.

Select an option

Save joeytwiddle/e7cccc1e3adc83512220078ec62d61e7 to your computer and use it in GitHub Desktop.

Revisions

  1. joeytwiddle revised this gist Jun 12, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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, concatenate it instead.
    - 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**

  2. joeytwiddle revised this gist May 29, 2025. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions logging_in_fastify.md
    Original 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:
  3. joeytwiddle revised this gist May 27, 2025. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions logging_in_fastify.md
    Original 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 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.
    - 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 unexpectedly **DO NOT WORK**
    ## Patterns which surprisingly **DO NOT WORK**

    ```javascript
    // Providing a string as the first argument? Only the first argument gets logged!
    // Does not log the data
    // This will not log the data
    fastify.log.info("message", { data })
    // Does not log the variable
    // This will not log the variable
    fastify.log.info("message", stringVariable)
    // Does not log the message
    // This will 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: {}
    // An error deep inside an object does not get serialized properly.
    // This will only log: msg: "test_log", error: {}
    fastify.log.info({ error }, "message")
    ```

  4. joeytwiddle revised this gist Apr 30, 2025. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions logging_in_fastify.md
    Original 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:

    // 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.
    ```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")
    ```
  5. joeytwiddle revised this gist Apr 30, 2025. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions logging_in_fastify.md
    Original 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")
    ```
  6. joeytwiddle revised this gist Mar 22, 2025. 1 changed file with 14 additions and 8 deletions.
    22 changes: 14 additions & 8 deletions logging_in_fastify.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,15 @@
    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.
    # 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**

    Patterns which are **WORKING**:
    ```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
    // This logs: msg: {baz: "qux"}, foo: "bar"
    fastify.log.debug({ foo: 'bar' }, { baz: 'qux' })
    // Through pino, this will appear as: {baz: "qux"}, foo: "bar"
    ```

    Patterns which unexpectedly **DO NOT WORK**:
    ## 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
  7. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion logging_in_fastify.md
    Original 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": {}
    // Only logs: msg: "test_log", error: {}
    fastify.log.info({ error }, "message")
    ```
  8. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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 serialised.
    // An error deep inside an object does not get serialized.
    // Only logs: "msg": "test_log", "error": {}
    fastify.log.info({ error }, "message")
    ```
  9. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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 }, "message")
    fastify.log.info({ data, foo, bar, baz }, "message")
    ```

    Patterns which unexpectedly **DO NOT WORK**:
  10. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions logging_in_fastify.md
    Original 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 **DO NOT WORK**:
    Patterns which unexpectedly **DO NOT WORK**:
    ```javascript
    // String as the first argument? Only the first argument gets logged!
    // Only logs the message, ignores the data object!
    // 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")

    // Error deep inside an object does not get serialised. It only logs: "msg":"test_log","error":{}
    // An error deep inside an object does not get serialised.
    // Only logs: "msg": "test_log", "error": {}
    fastify.log.info({ error }, "message")
    ```
  11. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions logging_in_fastify.md
    Original file line number Diff line number Diff line change
    @@ -16,13 +16,14 @@ fastify.log.info({ data }, "message")

    Patterns which **DO NOT WORK**:
    ```javascript
    // Two string arguments? Only the first argument get logged!
    // 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")

    // 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")
    ```
  12. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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: {} }
    // Error deep inside an object does not get serialised. It only logs: "msg":"test_log","error":{}
    fastify.log.info({ error }, "message")
    ```
  13. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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: { error: "[object Error]" }
    // Error deep inside an object? Only logs: message { error: {} }
    fastify.log.info({ error }, "message")
    ```
  14. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions logging_in_fastify.md
    Original file line number Diff line number Diff line change
    @@ -16,13 +16,13 @@ fastify.log.info({ data }, "message")

    Patterns which **DO NOT WORK**:
    ```javascript
    // Only the first arguments get logged!
    // Two string arguments? Only the first argument get logged!
    fastify.log.info("message", stringVariable)
    fastify.log.info(stringVariable, "message")

    // Only logs the message, ignores the data object!
    // Object as the second argument? Only logs the message, ignores the data object!
    fastify.log.info("message", { data })

    // Only logs: { error: "[object Error]" }
    // Error deep inside an object? Only logs: { error: "[object Error]" }
    fastify.log.info({ error }, "message")
    ```
  15. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions logging_in_fastify.md
    Original 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 })

  16. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions logging_in_fastify.md
    Original 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.)

    Working patterns:
    Patterns which are **WORKING**:
    ```javascript
    fastify.log.info("message")
    fastify.log.info(error, "message")
    fastify.log.info({ data }, "message")
    ```

    Patterns which don't work:
    Patterns which **DO NOT WORK**:
    ```javascript
    // Only logs the message, ignores the data object!
    fastify.log.info("message", { data })
  17. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions logging_in_fastify.md
    Original 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, 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)
    - 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
  18. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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 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)

  19. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions logging_in_fastify.md
    Original 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")

    // Only logs the message!
    fastify.log.info("message", { data })
    ```
  20. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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 the message string as the second argument!
    - 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)

  21. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging_in_fastify.md
    Original 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]"`
    // Only logs: { error: "[object Error]" }
    fastify.log.info({ error }, "message")

    // Only logs the message!
  22. joeytwiddle revised this gist Mar 4, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions logging_in_fastify.md
    Original 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")

  23. joeytwiddle renamed this gist Mar 4, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  24. joeytwiddle created this gist Mar 4, 2025.
    20 changes: 20 additions & 0 deletions gistfile1.txt
    Original 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 })
    ```