printk Log Levels and pr_* Helpers Explained
Learn the eight printk log levels, the pr_info and pr_err helpers, pr_fmt prefixes, rate limiting and the console loglevel that decides what reaches the screen.

The first debugging tool every kernel programmer uses is printk(). It is the kernel's printf: it formats a message and appends it to a ring buffer that you read with dmesg. But a message has a severity, the kernel decides from that severity whether it reaches the console, and a few helper macros save you from writing the severity by hand. This post explains the eight levels, the pr_* and dev_* helpers, the prefix macro, rate limiting and the settings that control what appears where, with code for a Linux 6.12 module and the exact dmesg output. The upstream description is printk basics, and the format specifiers are in How to get printk format specifiers right.
The eight levels
Each message carries a level from 0 to 7. Lower is more severe. The macros are string prefixes that printk() reads from the start of the format:
| Level | Macro | Helper | Meaning |
|---|---|---|---|
| 0 | KERN_EMERG | pr_emerg() | The system is unusable |
| 1 | KERN_ALERT | pr_alert() | Act immediately |
| 2 | KERN_CRIT | pr_crit() | Critical conditions |
| 3 | KERN_ERR | pr_err() | An error occurred |
| 4 | KERN_WARNING | pr_warn() | Something looks wrong |
| 5 | KERN_NOTICE | pr_notice() | Normal but significant |
| 6 | KERN_INFO | pr_info() | Informational |
| 7 | KERN_DEBUG | pr_debug() | Debug detail |
KERN_CONT and pr_cont() continue a line that a previous message left unfinished. They are fragile, because another CPU's message can land in the middle, so build the line in a buffer and print it once. A printk() with no prefix gets the default level, KERN_DEFAULT, which is normally warning.
Use the helpers, not raw printk(KERN_ERR ...). They are shorter and they pick up the module's prefix, described below. The practical guide to which level to use:
pr_err()for a failure the driver cannot recover from or that the user must act on.pr_warn()for a surprise that the driver handled.pr_info()for a one-time event such as probe success. Not for per-packet output.pr_debug()for detail that you want off by default.
A module that prints at every level
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/printk.h>
static int __init ticker_init(void)
{
pr_emerg("emergency test\n");
pr_alert("alert test\n");
pr_crit("critical test\n");
pr_err("error test: code %d\n", -5);
pr_warn("warning test\n");
pr_notice("notice test\n");
pr_info("loaded, period=%u ms\n", 250u);
pr_debug("debug test\n");
return 0;
}
static void __exit ticker_exit(void)
{
pr_info("unloaded\n");
}
module_init(ticker_init);
module_exit(ticker_exit);
MODULE_LICENSE("GPL");Load it and read the buffer with the raw level numbers shown, using dmesg -r:
sudo insmod ticker_printk.ko
sudo dmesg -r | tail -n 8<0>[ 5120.331204] ticker: emergency test
<1>[ 5120.331212] ticker: alert test
<2>[ 5120.331216] ticker: critical test
<3>[ 5120.331219] ticker: error test: code -5
<4>[ 5120.331222] ticker: warning test
<5>[ 5120.331225] ticker: notice test
<6>[ 5120.331229] ticker: loaded, period=250 msThe <N> prefix is the level, and the bracketed number is seconds since boot. Two things are missing from the output: the debug test line, because pr_debug() compiles to nothing unless the file is built with DEBUG defined or dynamic debug is enabled, and nothing at all on your terminal, unless your console loglevel lets the low-numbered messages through. Both are explained below.
pr_fmt: a prefix for every message
The #define pr_fmt(fmt) at the top, before any include, gives every pr_*() call in the file a prefix. KBUILD_MODNAME is the module's name as the build system sees it. Without it, a message says only loaded, period=250 ms and nobody can tell which driver wrote it. The macro must precede #include <linux/printk.h> directly or through another header, because pr_info() expands pr_fmt() at the point of use but the default definition is applied by the header when none exists.
For code that has a struct device, prefer the dev_* family from linux/dev_printk.h. They prefix the message with the driver and device names automatically and give user space the metadata to filter:
static int ticker_probe(struct platform_device *pdev)
{
dev_info(&pdev->dev, "probed, irq %d\n", platform_get_irq(pdev, 0));
dev_warn(&pdev->dev, "no clock specified, using default\n");
dev_err(&pdev->dev, "register read failed: %pe\n", ERR_PTR(-EIO));
return 0;
}ticker ticker.0: probed, irq 45
ticker ticker.0: no clock specified, using default
ticker ticker.0: register read failed: -EIOThe dev_dbg() form is compiled out the same way as pr_debug(). There are matching dev_emerg through dev_notice and dev_info variants, and for network code netdev_err() and friends. In a driver, almost every message should go through one of them.
Useful format specifiers
Kernel printk() has its own extensions to the usual printf set:
| Specifier | Prints | Example |
|---|---|---|
%pe | An ERR_PTR value as its error name | -ENOMEM |
%pS | A code address as symbol+offset | ticker_read+0x2a/0x90 |
%ps | A code address as the symbol name only | ticker_read |
%p | A hashed pointer, not the real address | 0000000012345678 |
%*ph | A small buffer as hex bytes | %*ph with length 4 prints de ad be ef |
%pa | A physical address (pass a pointer to it) | [mem 0x40000000] |
%zu, %zd | size_t and ssize_t | 4096 |
%pOF | A device tree node path | /soc/serial@7e215000 |
Printing an error code with %d is fine, but passing ERR_PTR(ret) to %pe prints the name, such as -EIO, which is easier to search for. Never use %s with a string that may be NULL or a user pointer, and never use %x for pointers: plain %p prints a hash on purpose, so a log does not give an attacker kernel addresses. Use %pS for function pointers when you want to know which function it is.
What reaches the console
Every message goes into the ring buffer regardless of level. Whether it also goes to the console, the serial port or the screen, depends on the console loglevel. A message prints on the console only when its level is numerically lower than the console loglevel. Read the setting:
cat /proc/sys/kernel/printk4 4 1 7The four numbers are the current console loglevel, the default level for messages with no prefix, the minimum allowed console loglevel and the boot-time default. With a console loglevel of 4, levels 0 to 3 appear on the console (emergency, alert, critical, error), and warnings and below are only in the buffer. Change it for the session with dmesg -n:
sudo dmesg -n 7
cat /proc/sys/kernel/printk7 4 1 7A level of 8 shows everything including debug. For permanent changes, set loglevel=7 on the kernel command line, or kernel.printk in sysctl.d. The quiet boot parameter lowers it to 4, and ignore_loglevel prints everything regardless, which is the setting to use when you debug an early crash. Remember that the console is separate from the ring buffer: dmesg shows all levels even when the console shows none.
Rate limiting and one-shot messages
A message in an interrupt handler or a loop can print thousands of lines a second, bury the log and slow the machine, since console output is slow. Three macros address that:
static irqreturn_t ticker_irq(int irq, void *data)
{
pr_err_ratelimited("ticker: fifo overrun\n"); /* bursts are limited */
pr_warn_once("ticker: first interrupt seen\n"); /* printed one time only */
return IRQ_HANDLED;
}The _ratelimited variants allow a burst of messages and then suppress the rest for an interval, defaulting to 10 messages per 5 seconds, and report how many were dropped as ticker: 128 callbacks suppressed. The _once variants print the first time that call site runs. printk_ratelimit() is the older condition-style form, and printk_ratelimited() takes a level. In a hot path, even a message that is rarely printed costs an evaluation of its arguments, so guard expensive formatting.
Do not use pr_info() for anything that fires per packet, per page or per interrupt. The right place for high-rate tracing is a trace event or a counter, not the log.
pr_debug and dynamic debug
pr_debug() and dev_dbg() produce no code in a normal build. Two ways turn them on. Define DEBUG at the top of the file, before the includes, to compile them in at level 7 and print always. Or build the kernel with CONFIG_DYNAMIC_DEBUG=y, which lets you switch individual call sites on at run time through a control file, covered in the dynamic debug post of this series. The second is the one to use in a shipped driver: leave the calls in, off by default, and enable them in the field.
Reading the buffer
The ring buffer has a fixed size, set by CONFIG_LOG_BUF_SHIFT and the log_buf_len= boot parameter, so old messages are overwritten on a busy system. dmesg reads it through /dev/kmsg and shows what is left. Timestamps are seconds since boot by default. If the system also forwards messages to journald, the journal keeps a longer history. Both tools are the subject of the next post in this series, and the earliest messages after boot are the first to be lost, so for crashes that happen early, a serial console or netconsole captures what the buffer cannot.
Common mistakes
A missing newline. A
printk()that does not end in a newline leaves the line open, and the next message joins it unless it starts with a level prefix, which makes the log misleading. End every message with a newline.Printing in a path that holds a lock the console needs. The console drivers take locks, and a
printk()under the wrong one can deadlock. Modern kernels defer console output through a printing thread, which reduces the risk, but avoid logging from inside the console or serial code you are debugging.**Using
%dfor asize_t.** Use%zu. The compiler warns, and on 32-bit builds the output is wrong.Putting secrets in the log. The ring buffer is readable by anyone unless
kernel.dmesg_restrictis set. Never print keys or passwords.**Leaving noisy
pr_info()calls.** If the driver prints on every operation, the real problems scroll away.


