'2009/07/13'에 해당되는 글 2건

  1. java.util.Timer 사용에 대한 좋은 원칙 2009/07/13
  2. sjava-logging 1.0 Level 2009/07/13
java.util.Timer에 대한 Good Practice에 대한 내용으로 http://tech.puredanger.com 블로그에 있는 내용입니다.
Two good rules of thumb for the use of Timer are:

1. Always start your Timer as a daemon thread. By default, new Timer() and other constructors use a non-daemon thread which means that the Timer will stick around forever and prevent shutdown. That may be what you expect, but I doubt it.
2. Always name your Timer. That name is attached to the Timer background thread. That way, if you do something dumb, and look at a thread dump, it will be exceedingly obvious when you’ve started 500 FooBarCleanupThreads.
위의 내용을 요약하면, "가능한 데몬 쓰레드로 실행을 시키고 Timer에 이름을 줘서 쓰레드 덤프등을 통해서 분명하게 디버깅을 할 수 있도록 하자"로 파악을 하였습니다.

그리고, http://tech.puredanger.com 블로그에 있는 내용의 댓글로 Timer를 사용하지 말고, ScheduledExecutorService를 사용하는 것이 좋다고 하네요.. 이유는 Timer(싱글 쓰레드)가 가지는 원천적인 문제를 해결하기 위해서라고 하네요.. 원본 내용이 짧기 때문에 읽어보세요.

ScheduledExecutorService의 간단한 예제는 http://www.java2s.com/Code/Java/Threads/Java1550newfeatureThreadSchedule.htm 를 참고하세요.. ^^

java.util.Timer의 schedule vs scheduleAtFixedRate에 대한 내용도 참고해 보세요. ^^

sjava-logging 1.0 Level

from My Project 2009/07/13 09:57
sjava-logging library는 level에 따른 로깅 가능여부를 체크하고 있지는 않습니다.
아래의 Level은 로깅파일의 분류 및 파일의 내용에 기입을 하기 위한 클래스입니다.
그리고, Level에 대한 사용을 위해서는 LevelFactory를 사용하면 됩니다.

LevelFactory.java
package net.sjava.logging;

import java.util.Map;
import java.util.HashMap;

/**
 *
 * @author mcsong@gmail.com
 * @since 2009. 7. 1.
 */
public class LevelFactory {   
    /** map of levels     */
    private Map<String, Level> levelMap;

    /** singleton instance */
    private static LevelFactory instance = new LevelFactory();
   
    /** constructor */
    private LevelFactory() {
        this.levelMap = new HashMap<String, Level>();
       
        this.levelMap.put("all", new Level(0, "all"));
        this.levelMap.put("fatal", new Level(1, "fatal"));
        this.levelMap.put("error", new Level(2, "error"));
        this.levelMap.put("warn", new Level(3, "warn"));
        this.levelMap.put("info", new Level(4, "info"));
        this.levelMap.put("debug", new Level(5, "debug"));
        this.levelMap.put("trace", new Level(6, "trace"));
        this.levelMap.put("system", new Level(7, "system"));
    }
   
    /**
     *
     * @return
     */
    public static LevelFactory getInstance() {
        return instance;
    }
   
    /**
     *
     * @param level
     * @return
     */
    public Level getLevel(int level) {
        switch (level) {
        case 1:
            return this.getLevel("fatal");
        case 2:
            return this.getLevel("error");
        case 3:
            return this.getLevel("warn");
        case 4:
            return this.getLevel("info");
        case 5:
            return this.getLevel("debug");
        case 6:
            return this.getLevel("trace");
        case 7:
            return this.getLevel("system");
           
        default:
            return this.getLevel("all");
        }
    }
   
    /**
     *
     * @param name
     * @return
     */
    public Level getLevel(String name) {
        if (name == null)
            return null;
       
        if(!this.levelMap.containsKey(name.toLowerCase()))
            return (Level)this.levelMap.get("all");
       
        return (Level)this.levelMap.get(name.toLowerCase());
    }
}

Level.java
package net.sjava.logging;

/**
 *
 * @author mcsong@gmail.com
 * @since 2009. 6. 19.
 */

public class Level {

    /** level number */
    public int level;
   
    /** level name */
    public String name;

    /**
     * Constructor
     *
     * @param level
     * @param name
     */
    public Level(int level, String name) {
        this.level = level;
        this.name = name;
    }
   
    /**
     * @return the level
     */
    public int getLevel() {
        return level;
    }

    /**
     * @param level the level to set
     */
    public void setLevel(int level) {
        this.level = level;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "name is " + this.name + ", level is " + this.level;
    }
}

'My Project' 카테고리의 다른 글

sjava-config 프로젝트의 소스 및 문서 위치 변경합니다.  (0) 2009/11/10
sjava-config 1.3  (0) 2009/10/23
sjava-logging 1.0 Level  (0) 2009/07/13
sjava-logging 1.0  (0) 2009/07/09
sjava-config 1.2  (0) 2009/07/08