diff src/goodjava/logger/Layouts.java @ 1448:6fc083e1d08c

start logger
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Feb 2020 18:14:32 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/Layouts.java	Sun Feb 23 18:14:32 2020 -0700
@@ -0,0 +1,51 @@
+package goodjava.logger;
+
+import java.io.StringWriter;
+import java.io.PrintWriter;
+
+
+public final class Layouts {
+	private Layouts() {}  // never
+
+	public static final Layout MESSAGE = new Layout() {
+		public String format(LoggingEvent event) {
+			return event.message;
+		}
+	};
+
+	public static final Layout LOGGER = new Layout() {
+		public String format(LoggingEvent event) {
+			return event.logger.name;
+		}
+	};
+
+	public static final Layout LEVEL = new Layout() {
+		public String format(LoggingEvent event) {
+			return Level.toString(event.level);
+		}
+	};
+
+	public static final Layout LEVEL_PADDED = new Layout() {
+		public String format(LoggingEvent event) {
+			return Level.toPaddedString(event.level);
+		}
+	};
+
+	public static final Layout THROWABLE = new Layout() {
+		public String format(LoggingEvent event) {
+			if( event.throwable == null )
+				return "";
+			StringWriter stringWriter = new StringWriter();
+			PrintWriter printWriter = new PrintWriter(stringWriter);
+			event.throwable.printStackTrace(printWriter);
+			return stringWriter.toString();
+		}
+	};
+
+	public static final Layout THREAD = new Layout() {
+		public String format(LoggingEvent event) {
+			return Thread.currentThread().getName();
+		}
+	};
+
+}