Containerization feature of SO
[so.git] / common / src / main / java / org / onap / so / logger / LoggerStartupListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.logger;
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25
26 import org.springframework.stereotype.Component;
27
28 import ch.qos.logback.classic.Level;
29 import ch.qos.logback.classic.Logger;
30 import ch.qos.logback.classic.LoggerContext;
31 import ch.qos.logback.classic.spi.LoggerContextListener;
32 import ch.qos.logback.core.Context;
33 import ch.qos.logback.core.spi.ContextAwareBase;
34 import ch.qos.logback.core.spi.LifeCycle;
35
36 @Component
37 public class LoggerStartupListener extends ContextAwareBase implements LoggerContextListener, LifeCycle {
38
39     private boolean started = false;
40
41     @Override
42     public void start() {
43         if (started) 
44                 return;
45         InetAddress addr= null;
46                 try {
47                         addr = InetAddress.getLocalHost();
48                 } catch (UnknownHostException e) {
49                         e.printStackTrace();
50                 }    
51         Context context = getContext();
52         if (addr != null) {
53                 context.putProperty("server.name", addr.getHostName());
54         }
55         started = true;
56     }
57
58     @Override
59     public void stop() {
60     }
61
62         @Override
63         public boolean isStarted() {
64                 return started;
65         }
66
67         @Override
68         public boolean isResetResistant() {
69                 return true;
70         }
71
72         @Override
73         public void onLevelChange(Logger arg0, Level arg1) {
74         }
75
76         @Override
77         public void onReset(LoggerContext arg0) {
78         }
79
80         @Override
81         public void onStart(LoggerContext arg0) {
82         }
83
84         @Override
85         public void onStop(LoggerContext arg0) {
86         }
87 }