View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.logging;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import com.google.inject.Inject;
8   
9   import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
10  
11  /**
12   * Class that wraps the map with log categories. It allows you
13   * to simply create new categories or query it's mapping.
14   * <p><p>
15   * The only constructor LogCategories(Logger) is protected and is instantiated
16   * during the construction of {@link AgentLogger}.
17   * 
18   * @author Jimmy
19   */
20  @AgentScoped
21  public class LogCategories extends AbstractLogCategories {
22  
23      private Map<String, LogCategory> categories = Collections.synchronizedMap(new HashMap<String, LogCategory>());
24  
25      @Inject
26      public LogCategories() {    	
27      }
28      
29      /**
30       * Returns existing category by the name or adds new one.
31       * <p><p>
32       * Note that new category doesn't have any handler appended,
33       * you have to create at least one for the category to produce something.
34       * <p>
35       * Example:<p>
36       * LogCategory myCategory = categories.getCategory("my log");  // creates new category
37       * myCategory.newHandler(new LogPublisher.ConsolePublisher()); // adds new handler with output to the console
38       *
39       * @param name
40       * @return
41       */
42      @Override
43      public LogCategory getCategory(String name) {
44          LogCategory category = getCategoriesInternal().get(name);
45          if (category != null) {
46              return category;
47          }
48          category = new LogCategory(name);
49          getCategoriesInternal().put(name, category);
50          return category;
51      }
52  
53      @Override
54      protected Map<String, LogCategory> getCategoriesInternal() {
55          return categories;
56      }
57  }