Code
Timestamped log line with level Regex
Most application logs start each line with a timestamp, then a severity level, then the message. This pattern matches that layout with an ISO-style date and time, optional fractional seconds and Z suffix, optional square brackets around the timestamp and the level, and the common levels TRACE, DEBUG, INFO, WARN, WARNING, ERROR and FATAL. The level is captured so you can count or filter by severity. It is useful for quick log triage, grep-style alerts and parsing logs into a table. It expects uppercase levels and a single-line message, so stack traces on following lines are not included.
Pattern (JavaScript)
/^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$/Starts at the start of the line/string, then a literal "[", optionally (zero or one time), then exactly 4 digits, then the character "-", then exactly 2 digits, then the character "-", then exactly 2 digits, then one of the characters T, then exactly 2 digits, then the character ":", then exactly 2 digits, then the character ":", then exactly 2 digits, then (the character ".", then a digit (0-9), between 1 and 6 times), optionally (zero or one time), then the character "Z", optionally (zero or one time), then a literal "]", optionally (zero or one time), then one or more whitespace characters, then a literal "[", optionally (zero or one time), then a group (the text "TRACE" OR the text "DEBUG" OR the text "INFO" OR the text "WARN" OR the text "WARNING" OR the text "ERROR" OR the text "FATAL"), then a literal "]", optionally (zero or one time), then one or more whitespace characters, then any character, one or more times, then the end of the line/string.
Examples
Matches
- ✓ 2026-11-30 14:05:22 ERROR Database connection refused
- ✓ [2026-11-30T09:00:01.123Z] [WARN] Disk usage at 85%
- ✓ 2026-01-15T08:30:00 INFO server started on port 3000
Does not match
- ✗ ERROR without timestamp
- ✗ 2026-11-30 14:05:22 NOTICE something happened
- ✗ 2026-11-30 14:05:22 error lowercase level
- ✗ 2026-11-30 ERROR missing time
Try it
0 matches (JavaScript engine)
2026-11-30 14:05:22 ERROR Database connection refused
[2026-11-30T09:00:01.123Z] [WARN] Disk usage at 85%
2026-01-15T08:30:00 INFO server started on port 3000
ERROR without timestamp
2026-11-30 14:05:22 NOTICE something happened
2026-11-30 14:05:22 error lowercase level
2026-11-30 ERROR missing timeCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$/g;
const text = "2026-11-30 14:05:22 ERROR Database connection refused";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$')
text = '2026-11-30 14:05:22 ERROR Database connection refused'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$/';
$text = '2026-11-30 14:05:22 ERROR Database connection refused';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^\\[?\\d{4}-\\d{2}-\\d{2}[ T]\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,6})?Z?\\]?\\s+\\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\\]?\\s+.+$");
String text = "2026-11-30 14:05:22 ERROR Database connection refused";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$`)
text := "2026-11-30 14:05:22 ERROR Database connection refused"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$");
var text = "2026-11-30 14:05:22 ERROR Database connection refused";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$").unwrap();
let text = "2026-11-30 14:05:22 ERROR Database connection refused";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^\[?\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z?\]?\s+\[?(TRACE|DEBUG|INFO|WARN|WARNING|ERROR|FATAL)\]?\s+.+$/
text = '2026-11-30 14:05:22 ERROR Database connection refused'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I find only errors and fatal lines?
- Replace the level alternation with (ERROR|FATAL). Everything else stays the same, so the pattern then matches only the high-severity lines.
- Why are lowercase levels rejected?
- Most logging libraries print levels in uppercase, and matching only uppercase avoids false hits on words like error inside messages. Add the i flag if your logger uses lowercase.
- Does it handle multi-line stack traces?
- No. It matches only the first line of an entry. Group following lines that do not start with a timestamp into the previous entry in code if you need full traces.