com.sector91.wit.log.DateAndMessageLogger.java Source code

Java tutorial

Introduction

Here is the source code for com.sector91.wit.log.DateAndMessageLogger.java

Source

// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //

////   ///   /// ///       
//////  ////   // ////  //// 
/// ////  ////  //  ////  //// 
///  //// /////  //        ///  
///  //// ///// //  //// ////// 
//   /// /////  //  ////  ////  
// //// ///// //  ////  ////   
/////////////  ////  ////   
////////////   ///   ///    
///// /////   ////  ////    
///// /////   //// ///// // 
////  ////    /// ///// //  
///// /////   ////////////   
////  ////     ////  ////    

// The Web framework with class.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //

// Copyright (c) 2013 Adam R. Nelson
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.sector91.wit.log;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public class DateAndMessageLogger implements Logger {

    private final DateTimeFormatter format;
    private final PrintStream out;

    public DateAndMessageLogger(OutputStream stream) {
        this(new PrintStream(stream));
    }

    public DateAndMessageLogger(PrintStream stream) {
        this(stream, ISODateTimeFormat.dateHourMinuteSecondMillis());
    }

    public DateAndMessageLogger(OutputStream stream, DateTimeFormatter format) {
        this(new PrintStream(stream), format);
    }

    public DateAndMessageLogger(PrintStream stream, DateTimeFormatter format) {
        this.out = stream;
        this.format = format;
    }

    @Override
    public void log(LogEntry entry) throws IOException {
        out.print(entry.time().toString(format));
        out.print(" ");
        out.println(entry.message());
    }
}