feat: ja4-platform monorepo — 5 services unified, tests & RPM builds standardized
Services: - ja4sentinel: TLS/JA4 fingerprint capture daemon (Go, libpcap) - logcorrelator: JA4 log correlation engine (Go, ClickHouse) - mod_reqin_log: Apache module (C, JSON request logging) - bot_detector: ML bot detection pipeline (Python) - dashboard: FastAPI/Streamlit analytics UI (Python) Shared libraries: - shared/go/ja4common: logger, config, shutdown, ipfilter (Go module) - shared/python/ja4_common: ClickHouseClient, ClickHouseSettings (Python package) - shared/clickhouse/: canonical SQL migrations (10 files) Build & packaging: - Unified 3-stage Dockerfile.package for Go RPMs (el8/el9/el10) - go.work workspace linking sentinel, correlator, ja4common - Makefile with test-all, build-all, rpm-* targets Fixes applied: - go.work: 1.21 → 1.24.6 (required by sentinel) - correlator Dockerfiles: golang:1.21 → golang:1.24 - replace directives in go.mod for ja4common local path - pyproject.toml: setuptools.backends → setuptools.build_meta - Removed static libpcap linking (unavailable on Rocky 9) - Fixed data races in output/writers_test.go (sync.Mutex + atomic.Int32) - Rewrote corrupted test files (logger_test.go × 2) Test coverage: - correlator: 67.1% total (unixsocket 80.5%, config 91.7%, app 83.3%, multi 87.7%, stdout 100%) - sentinel: all 10 packages pass (api, capture, config, fingerprint, ipfilter, logging, output, tlsparse) Documentation: - README.md + docs/ (architecture, development, 5 services, shared libs, DB schema & migrations) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
263
shared/go/ja4common/logger/logger.go
Normal file
263
shared/go/ja4common/logger/logger.go
Normal file
@ -0,0 +1,263 @@
|
||||
// Package logger provides unified structured logging for the ja4-platform services.
|
||||
// It merges the component-based logger from sentinel and the prefix/fields-based
|
||||
// logger from correlator into a single implementation.
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// LogLevel represents the severity of a log message.
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
DEBUG LogLevel = iota
|
||||
INFO
|
||||
WARN
|
||||
ERROR
|
||||
)
|
||||
|
||||
// ParseLogLevel converts a string to LogLevel.
|
||||
func ParseLogLevel(level string) LogLevel {
|
||||
switch strings.ToUpper(level) {
|
||||
case "DEBUG":
|
||||
return DEBUG
|
||||
case "INFO":
|
||||
return INFO
|
||||
case "WARN", "WARNING":
|
||||
return WARN
|
||||
case "ERROR":
|
||||
return ERROR
|
||||
default:
|
||||
return INFO
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the string representation of a LogLevel.
|
||||
func (l LogLevel) String() string {
|
||||
switch l {
|
||||
case DEBUG:
|
||||
return "DEBUG"
|
||||
case INFO:
|
||||
return "INFO"
|
||||
case WARN:
|
||||
return "WARN"
|
||||
case ERROR:
|
||||
return "ERROR"
|
||||
default:
|
||||
return "INFO"
|
||||
}
|
||||
}
|
||||
|
||||
// Logger provides structured prefix+fields-based logging (correlator style).
|
||||
type Logger struct {
|
||||
mu sync.RWMutex
|
||||
logger *log.Logger
|
||||
prefix string
|
||||
fields map[string]any
|
||||
minLevel LogLevel
|
||||
}
|
||||
|
||||
// New creates a new Logger with INFO level.
|
||||
func New(prefix string) *Logger {
|
||||
return &Logger{
|
||||
logger: log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds),
|
||||
prefix: prefix,
|
||||
fields: make(map[string]any),
|
||||
minLevel: INFO,
|
||||
}
|
||||
}
|
||||
|
||||
// NewWithLevel creates a new Logger with the specified minimum level.
|
||||
func NewWithLevel(prefix string, level string) *Logger {
|
||||
return &Logger{
|
||||
logger: log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds),
|
||||
prefix: prefix,
|
||||
fields: make(map[string]any),
|
||||
minLevel: ParseLogLevel(level),
|
||||
}
|
||||
}
|
||||
|
||||
// SetLevel sets the minimum log level.
|
||||
func (l *Logger) SetLevel(level string) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.minLevel = ParseLogLevel(level)
|
||||
}
|
||||
|
||||
// ShouldLog returns true if the given level should be logged.
|
||||
func (l *Logger) ShouldLog(level LogLevel) bool {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
return level >= l.minLevel
|
||||
}
|
||||
|
||||
// WithFields returns a new Logger with additional structured fields.
|
||||
func (l *Logger) WithFields(fields map[string]any) *Logger {
|
||||
l.mu.RLock()
|
||||
minLevel := l.minLevel
|
||||
prefix := l.prefix
|
||||
existing := make(map[string]any, len(l.fields))
|
||||
for k, v := range l.fields {
|
||||
existing[k] = v
|
||||
}
|
||||
l.mu.RUnlock()
|
||||
|
||||
for k, v := range fields {
|
||||
existing[k] = v
|
||||
}
|
||||
|
||||
return &Logger{
|
||||
logger: l.logger,
|
||||
prefix: prefix,
|
||||
fields: existing,
|
||||
minLevel: minLevel,
|
||||
}
|
||||
}
|
||||
|
||||
// Info logs an info message.
|
||||
func (l *Logger) Info(msg string) {
|
||||
if l.ShouldLog(INFO) {
|
||||
l.emit("INFO", msg)
|
||||
}
|
||||
}
|
||||
|
||||
// Infof logs a formatted info message.
|
||||
func (l *Logger) Infof(msg string, args ...any) {
|
||||
if l.ShouldLog(INFO) {
|
||||
l.emit("INFO", fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
|
||||
// Warn logs a warning message.
|
||||
func (l *Logger) Warn(msg string) {
|
||||
if l.ShouldLog(WARN) {
|
||||
l.emit("WARN", msg)
|
||||
}
|
||||
}
|
||||
|
||||
// Warnf logs a formatted warning message.
|
||||
func (l *Logger) Warnf(msg string, args ...any) {
|
||||
if l.ShouldLog(WARN) {
|
||||
l.emit("WARN", fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
|
||||
// Error logs an error message with an optional error value.
|
||||
func (l *Logger) Error(msg string, err error) {
|
||||
if !l.ShouldLog(ERROR) {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
l.emit("ERROR", msg+" "+err.Error())
|
||||
} else {
|
||||
l.emit("ERROR", msg)
|
||||
}
|
||||
}
|
||||
|
||||
// Debug logs a debug message.
|
||||
func (l *Logger) Debug(msg string) {
|
||||
if l.ShouldLog(DEBUG) {
|
||||
l.emit("DEBUG", msg)
|
||||
}
|
||||
}
|
||||
|
||||
// Debugf logs a formatted debug message.
|
||||
func (l *Logger) Debugf(msg string, args ...any) {
|
||||
if l.ShouldLog(DEBUG) {
|
||||
l.emit("DEBUG", fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) emit(level, msg string) {
|
||||
l.mu.RLock()
|
||||
prefix := l.prefix
|
||||
fields := make(map[string]any, len(l.fields))
|
||||
for k, v := range l.fields {
|
||||
fields[k] = v
|
||||
}
|
||||
l.mu.RUnlock()
|
||||
|
||||
var b strings.Builder
|
||||
if prefix != "" {
|
||||
b.WriteString("[")
|
||||
b.WriteString(prefix)
|
||||
b.WriteString("] ")
|
||||
}
|
||||
b.WriteString(level)
|
||||
b.WriteString(" ")
|
||||
b.WriteString(msg)
|
||||
|
||||
if len(fields) > 0 {
|
||||
keys := make([]string, 0, len(fields))
|
||||
for k := range fields {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
b.WriteString(" ")
|
||||
b.WriteString(k)
|
||||
b.WriteString("=")
|
||||
b.WriteString(fmt.Sprintf("%v", fields[k]))
|
||||
}
|
||||
}
|
||||
|
||||
l.logger.Print(b.String())
|
||||
}
|
||||
|
||||
// ComponentLogger wraps Logger to satisfy the sentinel-style component-based Logger interface.
|
||||
// This allows new services to use ja4common while sentinel's existing api.Logger interface
|
||||
// is still satisfied.
|
||||
type ComponentLogger struct {
|
||||
*Logger
|
||||
}
|
||||
|
||||
// NewComponentLogger creates a ComponentLogger with the specified log level.
|
||||
func NewComponentLogger(level string) *ComponentLogger {
|
||||
return &ComponentLogger{Logger: NewWithLevel("", level)}
|
||||
}
|
||||
|
||||
// Log emits a structured log entry for the given component.
|
||||
func (c *ComponentLogger) Log(component, level, message string, details map[string]string) {
|
||||
fields := make(map[string]any, len(details)+1)
|
||||
fields["component"] = component
|
||||
for k, v := range details {
|
||||
fields[k] = v
|
||||
}
|
||||
cl := c.Logger.WithFields(fields)
|
||||
switch strings.ToLower(level) {
|
||||
case "debug":
|
||||
cl.Debug(message)
|
||||
case "warn", "warning":
|
||||
cl.Warn(message)
|
||||
case "error":
|
||||
cl.Error(message, nil)
|
||||
default:
|
||||
cl.Info(message)
|
||||
}
|
||||
}
|
||||
|
||||
// Debug logs a debug entry for the given component.
|
||||
func (c *ComponentLogger) Debug(component, message string, details map[string]string) {
|
||||
c.Log(component, "debug", message, details)
|
||||
}
|
||||
|
||||
// Info logs an info entry for the given component.
|
||||
func (c *ComponentLogger) Info(component, message string, details map[string]string) {
|
||||
c.Log(component, "info", message, details)
|
||||
}
|
||||
|
||||
// Warn logs a warning entry for the given component.
|
||||
func (c *ComponentLogger) Warn(component, message string, details map[string]string) {
|
||||
c.Log(component, "warn", message, details)
|
||||
}
|
||||
|
||||
// Error logs an error entry for the given component.
|
||||
func (c *ComponentLogger) Error(component, message string, details map[string]string) {
|
||||
c.Log(component, "error", message, details)
|
||||
}
|
||||
Reference in New Issue
Block a user