Skip to content

Commit

Permalink
Provide individual PrintStream(s) for each logging Level in PrintStre…
Browse files Browse the repository at this point in the history
…amLogger, re #8
  • Loading branch information
safris committed Jan 10, 2024
1 parent 40bea47 commit e1cee3b
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 39 deletions.
107 changes: 68 additions & 39 deletions src/main/java/org/libj/logging/PrintStreamLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,66 @@ public class PrintStreamLogger implements Logger {
private static final String WARN = "[WARN] ";
private static final String ERROR = "[ERROR] ";

private final PrintStream ps;
private final PrintStream trace;
private final PrintStream debug;
private final PrintStream info;
private final PrintStream warn;
private final PrintStream error;
private final Level level;

/**
* Constructs a new {@link PrintStreamLogger} with the provided logging {@link Level} and {@link PrintStream}.
* Constructs a new {@link PrintStreamLogger} with the provided logging {@link Level} to be used with this logger, and the given
* {@link PrintStream}s to apply to each individual logging {@link Level}.
*
* @param level The logging {@link Level}.
* @param trace The {@link PrintStream} to be used for {@link Level#TRACE}.
* @param debug The {@link PrintStream} to be used for {@link Level#DEBUG}.
* @param info The {@link PrintStream} to be used for {@link Level#INFO}.
* @param warn The {@link PrintStream} to be used for {@link Level#WARN}.
* @param error The {@link PrintStream} to be used for {@link Level#ERROR}.
* @throws NullPointerException If any argument is null.
*/
public PrintStreamLogger(final Level level, final PrintStream trace, final PrintStream debug, final PrintStream info, final PrintStream warn, final PrintStream error) {
this.level = Objects.requireNonNull(level, "level is null");
this.trace = Objects.requireNonNull(trace, "trace is null");
this.debug = Objects.requireNonNull(debug, "debug is null");
this.info = Objects.requireNonNull(info, "info is null");
this.warn = Objects.requireNonNull(warn, "warn is null");
this.error = Objects.requireNonNull(error, "error is null");
}

/**
* Constructs a new {@link PrintStreamLogger} with the provided logging {@link Level} to be used with this logger, and the given
* {@link PrintStream} to apply to all logging {@link Level}s.
*
* @param level The logging {@link Level}.
* @param ps The {@link PrintStream}.
* @throws NullPointerException If {@code level} or {@code ps} is null.
* @throws NullPointerException If any argument is null.
*/
public PrintStreamLogger(final Level level, final PrintStream ps) {
this.level = Objects.requireNonNull(level, "level is null");
this.ps = Objects.requireNonNull(ps, "ps is null");
this.trace = this.debug = this.info = this.warn = this.error = Objects.requireNonNull(ps, "ps is null");
}

/**
* Constructs a new {@link PrintStreamLogger} with the provided logging {@link Level} to be used with this logger, with
* {@link System#out System.out} as the {@link PrintStream}.
* Constructs a new {@link PrintStreamLogger} with the provided logging {@link Level} to be used with this logger, and
* {@link System#out System.out} as the {@link PrintStream} for all logging {@link Level}s.
*
* @param level The logging {@link Level}.
* @throws NullPointerException If {@code level} is null.
*/
public PrintStreamLogger(final Level level) {
this(level, System.out);
this.level = Objects.requireNonNull(level, "level is null");
this.trace = this.debug = this.info = this.warn = this.error = System.out;
}

/**
* Constructs a new {@link PrintStreamLogger} with the {@link Level#INFO} to be used with this logger.
* Constructs a new {@link PrintStreamLogger} with the {@link Level#INFO} to be used with this logger, and {@link System#out
* System.out} as the {@link PrintStream} for all logging {@link Level}s.
*/
public PrintStreamLogger() {
this(Level.INFO, System.out);
this.level = Level.INFO;
this.trace = this.debug = this.info = this.warn = this.error = System.out;
}

@Override
Expand All @@ -79,32 +108,32 @@ public boolean isTraceEnabled() {
@Override
public void trace(final String msg) {
if (isTraceEnabled())
ps.println(TRACE + msg);
trace.println(TRACE + msg);
}

@Override
public void trace(final String format, final Object arg) {
if (isTraceEnabled())
ps.println(TRACE + String.format(format, arg));
trace.println(TRACE + String.format(format, arg));
}

@Override
public void trace(final String format, final Object arg1, final Object arg2) {
if (isTraceEnabled())
ps.println(TRACE + String.format(format, arg1, arg2));
trace.println(TRACE + String.format(format, arg1, arg2));
}

@Override
public void trace(final String format, final Object ... arguments) {
if (isTraceEnabled())
ps.println(TRACE + String.format(format, arguments));
trace.println(TRACE + String.format(format, arguments));
}

@Override
public void trace(final String msg, final Throwable t) {
if (isTraceEnabled()) {
ps.println(TRACE + msg);
t.printStackTrace(ps);
trace.println(TRACE + msg);
t.printStackTrace(trace);
}
}

Expand Down Expand Up @@ -146,32 +175,32 @@ public boolean isDebugEnabled() {
@Override
public void debug(final String msg) {
if (isDebugEnabled())
ps.println(DEBUG + msg);
debug.println(DEBUG + msg);
}

@Override
public void debug(final String format, final Object arg) {
if (isDebugEnabled())
ps.println(DEBUG + String.format(format, arg));
debug.println(DEBUG + String.format(format, arg));
}

@Override
public void debug(final String format, final Object arg1, final Object arg2) {
if (isDebugEnabled())
ps.println(DEBUG + String.format(format, arg1, arg2));
debug.println(DEBUG + String.format(format, arg1, arg2));
}

@Override
public void debug(final String format, final Object ... arguments) {
if (isDebugEnabled())
ps.println(DEBUG + String.format(format, arguments));
debug.println(DEBUG + String.format(format, arguments));
}

@Override
public void debug(final String msg, final Throwable t) {
if (isDebugEnabled()) {
ps.println(DEBUG + msg);
t.printStackTrace(ps);
debug.println(DEBUG + msg);
t.printStackTrace(debug);
}
}

Expand Down Expand Up @@ -213,32 +242,32 @@ public boolean isInfoEnabled() {
@Override
public void info(final String msg) {
if (isInfoEnabled())
ps.println(INFO + msg);
info.println(INFO + msg);
}

@Override
public void info(final String format, final Object arg) {
if (isInfoEnabled())
ps.println(INFO + String.format(format, arg));
info.println(INFO + String.format(format, arg));
}

@Override
public void info(final String format, final Object arg1, final Object arg2) {
if (isInfoEnabled())
ps.println(INFO + String.format(format, arg1, arg2));
info.println(INFO + String.format(format, arg1, arg2));
}

@Override
public void info(final String format, final Object ... arguments) {
if (isInfoEnabled())
ps.println(INFO + String.format(format, arguments));
info.println(INFO + String.format(format, arguments));
}

@Override
public void info(final String msg, final Throwable t) {
if (isInfoEnabled()) {
ps.println(INFO + msg);
t.printStackTrace(ps);
info.println(INFO + msg);
t.printStackTrace(info);
}
}

Expand Down Expand Up @@ -280,32 +309,32 @@ public boolean isWarnEnabled() {
@Override
public void warn(final String msg) {
if (isWarnEnabled())
ps.println(WARN + msg);
warn.println(WARN + msg);
}

@Override
public void warn(final String format, final Object arg) {
if (isWarnEnabled())
ps.println(WARN + String.format(format, arg));
warn.println(WARN + String.format(format, arg));
}

@Override
public void warn(final String format, final Object arg1, final Object arg2) {
if (isWarnEnabled())
ps.println(WARN + String.format(format, arg1, arg2));
warn.println(WARN + String.format(format, arg1, arg2));
}

@Override
public void warn(final String format, final Object ... arguments) {
if (isWarnEnabled())
ps.println(WARN + String.format(format, arguments));
warn.println(WARN + String.format(format, arguments));
}

@Override
public void warn(final String msg, final Throwable t) {
if (isWarnEnabled()) {
ps.println(WARN + msg);
t.printStackTrace(ps);
warn.println(WARN + msg);
t.printStackTrace(warn);
}
}

Expand Down Expand Up @@ -347,32 +376,32 @@ public boolean isErrorEnabled() {
@Override
public void error(final String msg) {
if (isErrorEnabled())
System.err.println(ERROR + msg);
error.println(ERROR + msg);
}

@Override
public void error(final String format, final Object arg) {
if (isErrorEnabled())
System.err.println(ERROR + String.format(format, arg));
error.println(ERROR + String.format(format, arg));
}

@Override
public void error(final String format, final Object arg1, final Object arg2) {
if (isErrorEnabled())
System.err.println(ERROR + String.format(format, arg1, arg2));
error.println(ERROR + String.format(format, arg1, arg2));
}

@Override
public void error(final String format, final Object ... arguments) {
if (isErrorEnabled())
System.err.println(ERROR + String.format(format, arguments));
error.println(ERROR + String.format(format, arguments));
}

@Override
public void error(final String msg, final Throwable t) {
if (isErrorEnabled()) {
System.err.println(ERROR + msg);
t.printStackTrace(System.err);
error.println(ERROR + msg);
t.printStackTrace(error);
}
}

Expand Down
76 changes: 76 additions & 0 deletions src/test/java/org/libj/logging/PrintStreamLoggerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Copyright (c) 2024 LibJ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* You should have received a copy of The MIT License (MIT) along with this
* program. If not, see <http://opensource.org/licenses/MIT/>.
*/

package org.libj.logging;

import static org.junit.Assert.*;

import org.junit.Test;
import org.slf4j.event.Level;

public class PrintStreamLoggerTest {
@Test
public void testException() {
try {
new PrintStreamLogger(null);
fail("Expected NullPointerException");
}
catch (final NullPointerException e) {
}

try {
new PrintStreamLogger(Level.DEBUG, null);
fail("Expected NullPointerException");
}
catch (final NullPointerException e) {
}

try {
new PrintStreamLogger(Level.DEBUG, null, System.out, System.out, System.out, System.out);
fail("Expected NullPointerException");
}
catch (final NullPointerException e) {
}

try {
new PrintStreamLogger(Level.DEBUG, System.out, null, System.out, System.out, System.out);
fail("Expected NullPointerException");
}
catch (final NullPointerException e) {
}

try {
new PrintStreamLogger(Level.DEBUG, System.out, System.out, null, System.out, System.out);
fail("Expected NullPointerException");
}
catch (final NullPointerException e) {
}

try {
new PrintStreamLogger(Level.DEBUG, System.out, System.out, System.out, null, System.out);
fail("Expected NullPointerException");
}
catch (final NullPointerException e) {
}

try {
new PrintStreamLogger(Level.DEBUG, System.out, System.out, System.out, System.out, null);
fail("Expected NullPointerException");
}
catch (final NullPointerException e) {
}
}
}

0 comments on commit e1cee3b

Please sign in to comment.