Logging - Laaangweilig!

Ein Log

Speyer Handlog“ von Lokilech - Eigenes Werk. Lizenziert unter CC BY-SA 3.0 über Wikimedia Commons.

Logging für Entwickler

As personal choice, we tend not to use debuggers […] we find stepping through a program less productive than […] adding output statements and self-checking code at critical places. It takes less time to decide where to put print statements than to single-step to the critical section of code, even assuming we know where that is. More important, debugging statements stay with the program; debugging sessions are transient.

(Brian W. Kerninghan, Rob Pike: "The Practice of Programming")

Logging für den Betrieb

Most developers implement logging as though they are the primary consumer of the log files. In fact, administrators and engineers in operations will spend far more time with these log files than developers will.
(Michael T. Nygard: "Release It!")

Log-Level

log4j

  • FATAL
  • ERROR
  • WARN
  • INFO
  • DEBUG
  • TRACE

java.util.logging

  • SEVERE
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST

Betrieb

  • ERROR
  • WARN
  • INFO

Die Meldungen müssen ohne den Source-Code verständlich sein.

Log-Meldungen in diesen Leveln sind ein Applikations-Feature.

INFO

Eine Operation war erfolgreich, das System ist funktionsfähig

  • Rückmeldung über Betriebs-Konfigurationen (DB-Connect-String)
  • erfolgreiche Zustandsänderungen (Systemstart beendet)

WARN

Eine Operation ist fehlgeschlagen, das System ist funktionsfähig

  • fachliche Fehler (erfolgloser Login)
  • behebbare technische Fehler (Switch auf Backup-DB)
  • Schwellwert-Verletzungen

ERROR

Eine Operation ist fehlgeschlagen, das System ist nicht funktionsfähig

  • technische Fehler (DB-Verbindung verloren)

Entwickler

  • DEBUG
  • TRACE

Die Meldungen sind vielleicht nur mit Kenntnis des Source-Codes verständlich.

DEBUG

Information für den Entwickler, die längerfristig interessant bleiben.

  • Interaktionen an Systemgrenzen (Server-Aufruf)
  • Feingranulare Zustandsänderungen

Die Meldungen bilden gegebenenfalls die Grundlage für Entwickler-Support am Produktionssystem

TRACE

Information für den Entwickler während der Entwicklung

  • Alles mögliche
  • Auf Performance achten

Log-Destination

  • Konsole
  • Datei
  • Log-Service

Loggen auf die Konsole

  • Das Konsole-Gerät ist praktisch immer verfügbar
  • Sehr einfach zu konfigurieren
  • nicht persistent
  • nicht durchsuchbar

Manche Systeme machen aus der Not eine Tugend, z.B. Tomcats logs/catalina.out.

Loggen in eine Datei

  • … aber in welche Datei?
  • Existiert das Verzeichnis - unter UNIX / unter Windows?
  • Synchronisation von vielen Loggern in eine Datei problematisch.
  • Viele Log-Dateien sind untereinander schwer zu korrellieren.

Loggen in einen Log-Service

  • Syslog
  • Windows Event Log
  • Netzwerk / Log-Server
  • Splunk, Logstash, …

Die einzige Lösung, wenn der Betrieb mehr als vier Server und mehr als vier Sinne beisammen hat.

Fazit

Während der Entwicklung wird festgelegt, was an Logging zur Verfügung steht.

Während des Betriebes und der Entwicklung wird konfiguriert:

  • Was wird nicht geloggt → Log-Level
  • Wohin wird geloggt → Log-Destination
  • Wie wird geloggt → Log-Format

Auf geht's: https://github.com/TNG/logging-workshop oder
git clone https://github.com/TNG/logging-workshop.git

log4j-Konfiguration

The preferred way to specify the default initialization file is through the log4j.configuration system property. (Short introduction to log4j)

Loggen auf Konsole


<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out"/>
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
		</layout>
	</appender>
	<root>
		<priority value ="debug" />
		<appender-ref ref="console" />
	</root>
</log4j:configuration>
				

Loggen in Datei


<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
	<appender name="util" class="org.apache.log4j.FileAppender">
		<param name="File" value="util.log" />
		<param name="Append" value="true" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%t %-5p %c{2} - %m%n" />
		</layout>
	</appender>
	<root>
		<priority value="debug" />
		<appender-ref ref="util" />
	</root>
</log4j:configuration>