Logger.Backends.Logfmt.Quoter (LoggerLogfmt v1.0.1)

View Source

Handles quoting and escaping of values for logfmt format.

This module determines whether a value needs quoting and/or escaping according to the logfmt specification and applies the necessary transformations.

Quoting Rules

A value needs quoting if it contains:

  • Spaces
  • Equal signs (=)
  • Backslashes ()
  • Control characters (0x00-0x1F, 0x7F)
  • Double quotes (")

Escaping

Special characters are escaped using standard escape sequences:

  • \t for tab
  • \n for newline
  • \r for carriage return
  • \" for double quote
  • \\ for backslash
  • \uXXXX for control characters

Examples

iex> Logger.Backends.Logfmt.Quoter.maybe_quote("simple")
"simple"

iex> Logger.Backends.Logfmt.Quoter.maybe_quote("hello world")
[?", "hello world", ?"]

iex> Logger.Backends.Logfmt.Quoter.maybe_quote("has\"quote")
[?", "has\\\"quote", ?"]

Summary

Functions

Escapes special characters in a string.

Determines the quoting strategy needed for a value.

Quotes a value if necessary based on its content.

Functions

escape(val)

Escapes special characters in a string.

Parameters

  • val - The string to escape

Returns

A string with all special characters properly escaped.

Examples

iex> Logger.Backends.Logfmt.Quoter.escape("hello\nworld")
"hello\\nworld"

iex> Logger.Backends.Logfmt.Quoter.escape("has\"quote")
"has\\\"quote"

infer_quote(val)

Determines the quoting strategy needed for a value.

Parameters

  • val - The string value to analyze

Returns

  • :none - No quoting needed
  • :quoting - Needs quotes but no escaping
  • :quoting_and_escaping - Needs both quotes and escaping

Examples

iex> Logger.Backends.Logfmt.Quoter.infer_quote("simple")
:none

iex> Logger.Backends.Logfmt.Quoter.infer_quote("has space")
:quoting

iex> Logger.Backends.Logfmt.Quoter.infer_quote("has\"quote")
:quoting_and_escaping

maybe_quote(val)

Quotes a value if necessary based on its content.

Parameters

  • val - The string value to potentially quote

Returns

  • The original string if no quoting is needed
  • An iolist with quotes if quoting is needed
  • An iolist with quotes and escaped content if escaping is needed

Examples

iex> Logger.Backends.Logfmt.Quoter.maybe_quote("noSpaces")
"noSpaces"

iex> Logger.Backends.Logfmt.Quoter.maybe_quote("has space")
[34, "has space", 34]