001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.log; 017 018import static com.google.common.base.Preconditions.checkNotNull; 019 020import java.util.List; 021 022import org.slf4j.Logger; 023 024import com.google.common.collect.ImmutableList; 025 026public final class LoggerContext { 027 028 private final LoggerLevel level; 029 private final String msg; 030 private final ImmutableList<Object> args; 031 private final Logger logger; 032 033 private LoggerContext(Builder builder) { 034 this.logger = builder.logger; 035 this.level = builder.level; 036 this.msg = builder.msg; 037 this.args = ImmutableList.copyOf(builder.args); 038 } 039 040 public static Builder builder(Logger logger, String msg) { 041 return new Builder(logger, msg); 042 } 043 044 public static class Builder { 045 046 public Builder(Logger logger, String msg) { 047 this.logger = logger; 048 this.msg = msg; 049 } 050 051 // Required 052 private Logger logger; 053 private final String msg; 054 055 // Optional 056 private LoggerLevel level = LoggerLevel.INFO; 057 private List<Object> args = ImmutableList.of(); 058 059 public Builder level(LoggerLevel level) { 060 this.level = level; 061 return this; 062 } 063 064 public Builder args(List<Object> args) { 065 this.args = args; 066 return this; 067 } 068 069 public LoggerContext build() { 070 LoggerContext instance = new LoggerContext(this); 071 validate(instance); 072 return instance; 073 } 074 075 private static void validate(LoggerContext instance) { 076 checkNotNull(instance.logger, "logger cannot be null"); 077 checkNotNull(instance.level, "level cannot be null"); 078 checkNotNull(instance.msg, "msg cannot be null"); 079 checkNotNull(instance.args, "args cannot be null"); 080 } 081 } 082 083 public LoggerLevel getLevel() { 084 return level; 085 } 086 087 public String getMsg() { 088 return msg; 089 } 090 091 public ImmutableList<Object> getArgs() { 092 return args; 093 } 094 095 public Logger getLogger() { 096 return logger; 097 } 098 099}