View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you cannot use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.log;
17  
18  import static com.google.common.base.Preconditions.checkNotNull;
19  
20  import java.util.List;
21  
22  import org.slf4j.Logger;
23  
24  import com.google.common.collect.ImmutableList;
25  
26  public final class LoggerContext {
27  
28  	private final LoggerLevel level;
29  	private final String msg;
30  	private final ImmutableList<Object> args;
31  	private final Logger logger;
32  
33  	private LoggerContext(Builder builder) {
34  		this.logger = builder.logger;
35  		this.level = builder.level;
36  		this.msg = builder.msg;
37  		this.args = ImmutableList.copyOf(builder.args);
38  	}
39  
40  	public static Builder builder(Logger logger, String msg) {
41  		return new Builder(logger, msg);
42  	}
43  
44  	public static class Builder {
45  
46  		public Builder(Logger logger, String msg) {
47  			this.logger = logger;
48  			this.msg = msg;
49  		}
50  
51  		// Required
52  		private Logger logger;
53  		private final String msg;
54  
55  		// Optional
56  		private LoggerLevel level = LoggerLevel.INFO;
57  		private List<Object> args = ImmutableList.of();
58  
59  		public Builder level(LoggerLevel level) {
60  			this.level = level;
61  			return this;
62  		}
63  
64  		public Builder args(List<Object> args) {
65  			this.args = args;
66  			return this;
67  		}
68  
69  		public LoggerContext build() {
70  			LoggerContext instance = new LoggerContext(this);
71  			validate(instance);
72  			return instance;
73  		}
74  
75  		private static void validate(LoggerContext instance) {
76  			checkNotNull(instance.logger, "logger cannot be null");
77  			checkNotNull(instance.level, "level cannot be null");
78  			checkNotNull(instance.msg, "msg cannot be null");
79  			checkNotNull(instance.args, "args cannot be null");
80  		}
81  	}
82  
83  	public LoggerLevel getLevel() {
84  		return level;
85  	}
86  
87  	public String getMsg() {
88  		return msg;
89  	}
90  
91  	public ImmutableList<Object> getArgs() {
92  		return args;
93  	}
94  
95  	public Logger getLogger() {
96  		return logger;
97  	}
98  
99  }