Configuration
icecream
's behavior can be customized using the ic.configureOutput()
method. This allows you to change the output prefix, redirect the output, and control context information.
ic.configureOutput()
This is the central function for all configuration. You can set one or more of the following parameters.
prefix
Controls the string that appears at the beginning of every ic()
output. The default is ic|
.
The prefix can be a static string or a callable (like a function) that returns a string.
Static String Prefix:
from icecream import ic
ic.configureOutput(prefix='Debug -> ')
ic('world')
Output:
Debug -> 'world'
Dynamic Function Prefix:
import time
from icecream import ic
def timestamp_prefix():
return f'{time.strftime("%H:%M:%S")} |> '
ic.configureOutput(prefix=timestamp_prefix)
ic('world')
Output:
14:35:10 |> 'world': 'world'
outputFunction
Redirects ic()
's output. By default, icecream
prints to sys.stderr
. You can provide any function that accepts a single string argument.
This is useful for integrating with Python's logging
module.
import logging
from icecream import ic
logging.basicConfig(level=logging.WARNING, format='%(message)s')
ic.configureOutput(outputFunction=logging.warning)
ic('This is a warning.')
Output:
ic| 'This is a warning.': 'This is a warning.'
argToStringFunction
Sets a custom function to serialize all arguments to strings. For more fine-grained control over specific types, it's recommended to use icecream.argumentToString.register()
as described in the Advanced Usage guide.
from icecream import ic
def custom_formatter(obj):
if isinstance(obj, str):
return f'[string of length {len(obj)}]'
return repr(obj)
ic.configureOutput(argToStringFunction=custom_formatter)
ic(7, 'hello')
Output:
ic| 7: 7, 'hello': [string of length 5]
includeContext
Set to True
to include the filename, line number, and parent function in the output for all ic()
calls, even those with arguments. It is False
by default.
from icecream import ic
ic.configureOutput(includeContext=True)
def my_func():
i = 42
ic(i)
my_func()
Output:
ic| your_script.py:7 in my_func()- i: 42
contextAbsPath
When includeContext
is True
, setting contextAbsPath
to True
will display the full, absolute path to the file instead of just the filename. This is useful in complex projects with duplicate filenames and can be made clickable in many modern editors (like VS Code).
from icecream import ic
ic.configureOutput(includeContext=True, contextAbsPath=True)
def my_func():
i = 42
ic(i)
my_func()
Output:
ic| /path/to/your/project/your_script.py:7 in my_func()- i: 42
lineWrapWidth
Controls the maximum character width of a line before icecream
attempts to wrap the output onto multiple lines. The default is 70
characters.
from icecream import ic
ic.configureOutput(lineWrapWidth=40)
name = "Montgomery Burns"
occupation = "Nuclear Power Plant Owner"
ic(name, occupation)
Output (wrapped):
ic| name: 'Montgomery Burns'
occupation: 'Nuclear Power Plant Owner'