Java/Velocity/Velocity Log

Материал из Java эксперт
Версия от 07:07, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Console Log System

import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.log.LogSystem;
public class ConsoleLogSystem implements LogSystem {
  private RuntimeServices rs;
  private int maxLevel = LogSystem.INFO_ID;
  private static final String[] LEVEL_NAMES = new String[] { "ERROR", "WARN",
      "INFO", "DEBUG" };
  private static final int[] LEVELS = new int[] { LogSystem.ERROR_ID,
      LogSystem.WARN_ID, LogSystem.INFO_ID, LogSystem.DEBUG_ID };
  public void init(RuntimeServices rs) throws Exception {
    System.out.println("ConsoleLogSystem.init() called");
    this.rs = rs;
    configure();
  }
  public void logVelocityMessage(int level, String message) {
    if (level >= maxLevel) {
      System.out.println("[" + getLevelName(level) + "] " + message);
    }
  }
  private void configure() {
    String maxLevelName = rs.getString("console.logsystem.max.level");
    int level = getLevelFromString(maxLevelName);
    if (level > -1) {
      System.out.println("Using log level: " + maxLevelName);
      maxLevel = level;
    }
  }
  private int getLevelFromString(String levelName) {
    for (int x = 0; x < LEVEL_NAMES.length; x++) {
      if (LEVEL_NAMES[x].equals(levelName)) {
        return LEVELS[x];
      }
    }
    // should not arrive here, couldn"t find the level
    return -1;
  }
  private String getLevelName(int level) {
    for (int x = 0; x < LEVELS.length; x++) {
      if (LEVELS[x] == level) {
        return LEVEL_NAMES[x];
      }
    }
    return "UNKNOWN";
  }
}





Custom log for Velocity

   <source lang="java">

/*

* Copyright 2000-2001,2004 The Apache Software Foundation.
* 
* 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.
*/

import org.apache.velocity.app.Velocity; import org.apache.velocity.VelocityContext; import org.apache.velocity.Template; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; import java.io.*; import java.util.ArrayList; /**

* This class is a simple demonstration of how the Velocity Template Engine
* can be used in a standalone application.
*
* @author 


How to use an existing Log4j Category as the Velocity logging target

   <source lang="java">

/*

* Copyright 2000-2001,2004 The Apache Software Foundation.
* 
* 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.
*/

import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Category; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.runtime.RuntimeConstants;

/**

*  Simple example class to show how to use an existing Log4j Categeory
*  as the Velocity logging target.
*
* @author 


This is a toy demonstration of how Velocity can use an externally configured logger

   <source lang="java">

/*

* Copyright 2000-2001,2004 The Apache Software Foundation.
* 
* 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.
*/

import org.apache.velocity.app.Velocity; import org.apache.velocity.runtime.log.LogSystem; import org.apache.velocity.runtime.RuntimeServices;

/**

*  This is a toy demonstration of how Velocity
*  can use an externally configured logger.  In 
*  this example, the class using Velocity
*  implements Velocity"s logger interface, and
*  all Velocity log messages are funneled back
*  through it.
*
* @author