Package anvil.server.basic

Source Code of anvil.server.basic.BasicLogDevice

/*
* $Id: BasicLogDevice.java,v 1.6 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server.basic;

import java.io.IOException;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

import anvil.LogEvent;
import anvil.LogDevice;
import anvil.LogLayout;
import anvil.server.Zone;
import anvil.server.LoggingPreferences;
import anvil.server.ConfigurationError;

/**
* class BasicLogDevice
*
* @author  Jani Lehtim�ki
*/
public class BasicLogDevice implements LogDevice
{

  private String    _target;
  private LogLayout _layout;
  private OutputStream _output;
  private boolean _flush = true;


  public BasicLogDevice()
  {
  }
 
 
  public void initialize(Zone zone)
  {
    LoggingPreferences prefs = zone.getLoggingPreferences();
   
    String s = prefs.getFormat();
    if (s.equals("basic")) {
      _layout = new BasicLogLayout(prefs);
    } else if (s.indexOf('.')>0) {
      try {
        _layout = (LogLayout)Class.forName(s).newInstance();
      } catch (ConfigurationError e) {
        e.fillInStackTrace();
        throw e;
      } catch (Throwable t) {
        throw new ConfigurationError("Couldn't create log layout: "+s+", reason: "+t.toString());
      }
    } else {
      throw new ConfigurationError("Unknown log layout '"+_layout+"'");
    }
   
    _target = prefs.getTarget();
   
    if (_target.equals("stderr")) {   
      _output = System.err;
      _flush = false;
     
    } else if (_target.equals("stdout")) {   
      _output = System.out;
      _flush = false;
     
    } else {
      try {
        _output = new BufferedOutputStream(new FileOutputStream(_target));
      } catch (IOException e) {
        throw new ConfigurationError("Couldn't open log file '"+_target+"'");
      }
    }
  }
 

  public String toString()
  {
    return _layout.toString() + "@" + _target;
  }


  public void stop()
  {
    try {
      if (_flush) {
        _output.flush();
        _output.close();
      }
      _output = null;
    } catch (Throwable t) {
    }
  }
 

  public void setLayout(LogLayout layout)
  {
    _layout = layout;
  }


  public void logEvent(LogEvent event)
  {
    if (_output != null) {
      try {
        String message = _layout.format(event);
        _output.write(anvil.util.Conversions.getBytes(message));
        if (_flush) {
          _output.flush();
        }
      } catch (IOException e) {
        anvil.Log.log().alert("Logging failed", e);
      } catch (Throwable t) {
      }
    }
  }
 
 
}
TOP

Related Classes of anvil.server.basic.BasicLogDevice

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.