home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (Logger.java)

This example Groovy source code file (Logger.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

class, debug, error, io, io, logger, logger, object, object, red, string, string, throwable, throwable, warn

The Groovy Logger.java source code

 * Copyright 2003-2007 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.codehaus.groovy.tools.shell.util;

import org.codehaus.groovy.tools.shell.IO;

import java.io.IOException;

import static org.fusesource.jansi.Ansi.ansi;
import static org.fusesource.jansi.Ansi.Color;
import static org.fusesource.jansi.Ansi.Color.*;
import static org.fusesource.jansi.Ansi.Attribute.*;

 * Provides a very, very basic logging API.
 * @version $Id: Logger.java 20937 2010-10-11 20:01:54Z paulk $
 * @author <a href="mailto:jason@planet57.com">Jason Dillon
public final class Logger {
    public static IO io;
    public final String name;

    private Logger(final String name) {
        assert name != null;
        this.name = name;
    private void log(final String level, Object msg, Throwable cause) {
        assert level != null;
        assert msg != null;
        if (io == null) {
            io = new IO();

        // Allow the msg to be a Throwable, and handle it properly if no cause is given
        if (cause == null) {
            if (msg instanceof Throwable) {
                cause = (Throwable) msg;
                msg = cause.getMessage();

        Color color = GREEN;
        if (WARN.equals(level) || ERROR.equals(level)) {
            color = RED;

        io.out.println(ansi().a(INTENSITY_BOLD).a(color).a(level).reset().a(" [").a(name).a("] ").a(msg));

        if (cause != null) {

        try {
        } catch (IOException io) {
            throw new RuntimeException(io);
    // Level helpers
    private static final String DEBUG = "DEBUG";

    public boolean isDebugEnabled() {
        return Preferences.verbosity == IO.Verbosity.DEBUG;

    public boolean isDebug() {
        return isDebugEnabled();
    public void debug(final Object msg) {
        if (isDebugEnabled()) {
            log(DEBUG, msg, null);
    public void debug(final Object msg, final Throwable cause) {
        if (isDebugEnabled()) {
            log(DEBUG, msg, cause);

    private static final String WARN = "WARN";

    public void warn(final Object msg) {
        log(WARN, msg, null);

    public void warn(final Object msg, final Throwable cause) {
        log(WARN, msg, cause);
    private static final String ERROR = "ERROR";

    public void error(final Object msg) {
        log(ERROR, msg, null);

    public void error(final Object msg, final Throwable cause) {
        log(ERROR, msg, cause);

    // Factory access
    public static Logger create(final Class type) {
        return new Logger(type.getName());

    public static Logger create(final Class type, final String suffix) {
        return new Logger(type.getName() + "." + suffix);

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy Logger.java source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.