1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.slf4j.impl;
20
21 import java.util.Optional;
22 import org.apache.maven.logwrapper.LogLevelRecorder;
23 import org.apache.maven.logwrapper.MavenSlf4jWrapperFactory;
24 import org.slf4j.Logger;
25
26
27
28
29 public class MavenLoggerFactory extends SimpleLoggerFactory implements MavenSlf4jWrapperFactory {
30 private LogLevelRecorder logLevelRecorder = null;
31
32 @Override
33 public void setLogLevelRecorder(LogLevelRecorder logLevelRecorder) {
34 if (this.logLevelRecorder != null) {
35 throw new IllegalStateException("LogLevelRecorder has already been set.");
36 }
37
38 this.logLevelRecorder = logLevelRecorder;
39 }
40
41 @Override
42 public Optional<LogLevelRecorder> getLogLevelRecorder() {
43 return Optional.ofNullable(logLevelRecorder);
44 }
45
46
47
48
49 @Override
50 public Logger getLogger(String name) {
51 Logger simpleLogger = loggerMap.get(name);
52 if (simpleLogger != null) {
53 return simpleLogger;
54 } else {
55 Logger newInstance = getNewLoggingInstance(name);
56 Logger oldInstance = loggerMap.putIfAbsent(name, newInstance);
57 return oldInstance == null ? newInstance : oldInstance;
58 }
59 }
60
61 private Logger getNewLoggingInstance(String name) {
62 if (logLevelRecorder == null) {
63 return new MavenSimpleLogger(name);
64 } else {
65 return new MavenFailOnSeverityLogger(name, logLevelRecorder);
66 }
67 }
68 }