2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.mso.logger;
24 import java.io.BufferedReader;
25 import java.io.BufferedWriter;
27 import java.io.FileReader;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.io.StringWriter;
32 import java.net.InetAddress;
33 import java.net.UnknownHostException;
34 import java.util.UUID;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
40 import org.openecomp.mso.entity.MsoRequest;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.att.eelf.i18n.EELFResolvableErrorEnum;
45 import java.text.DateFormat;
46 import java.text.SimpleDateFormat;
47 import java.util.Date;
50 * Simple wrapper around the EELF Logger class for MSO usage. This class
51 * supports all of the normal logging functions (debug, info, etc.), prepending
52 * a string of format "[<requestId>|<serviceId]" to each message.
54 * MSO code should initialize with these IDs when available, so that individual
55 * requests and/or services can be tracked throughout the various MSO component
56 * logs (API Handler, BPEL, and Adapters).
60 public class MsoLogger {
62 public static final String REQUEST_ID = "RequestId";
63 public static final String SERVICE_INSTANCE_ID = "ServiceInstanceId";
64 public static final String SERVICE_NAME = "ServiceName";
65 private static final String SERVICE_NAME_IS_METHOD_NAME = "ServiceNameIsMethodName";
66 private static final String INSTANCE_UUID = "InstanceUUID";
67 private static final String SERVER_IP = "ServerIPAddress";
68 private static final String FQDN = "ServerFQDN";
69 public static final String REMOTE_HOST = "RemoteHost";
70 public static final String ALERT_SEVERITY = "AlertSeverity";
71 public static final String TIMER = "Timer";
72 private static final String USER = "User";
73 private static final String DUMMY_VALUE = "trace-#";
74 public static final String UNKNOWN = "UNKNOWN";
75 //For getting an identity of calling application
76 public static final String HEADER_FROM_APP_ID = "X-FromAppId";
77 public static final String FROM_APP_ID = "FromAppId";
78 // Audit/Metric log specific
79 private static final String BEGINTIME = "BeginTimestamp";
80 private static final String ENDTIME = "EndTimestamp";
81 public static final String PARTNERNAME = "PartnerName";
82 private static final String STATUSCODE = "StatusCode";
83 private static final String RESPONSECODE = "ResponseCode";
84 private static final String RESPONSEDESC = "ResponseDesc";
85 // Metric log specific
86 private static final String TARGETENTITY = "TargetEntity";
87 private static final String TARGETSERVICENAME = "TargetServiceName";
88 private static final String TARGETVIRTUALENTITY = "TargetVirtualEntity";
90 private static final String FATAL_LEVEL = "FATAL";
91 private static final String ERROR_LEVEL = "ERROR";
92 private static final String WARN_LEVEL = "WARN";
93 private static final String INFO_LEVEL = "INFO";
94 private static final String DEBUG_LEVEL = "DEBUG";
96 private static final String ERRORCODE = "ErrorCode";
97 private static final String ERRORDESC = "ErrorDesc";
100 APIH, BPEL, RA, ASDC, GENERAL
103 public enum StatusCode {
107 public enum ResponseCode {
108 Suc(0), PermissionError(100), DataError(300), DataNotFound(301), BadRequest(302), SchemaError(
109 400), BusinessProcesssError(500), ServiceNotAvailable(501), InternalError(
110 502), Conflict(503), DBAccessError(504), CommunicationError(505), UnknownError(900);
114 public int getValue() {
118 private ResponseCode(int value) {
123 public enum ErrorCode {
124 PermissionError(100), AvailabilityError(200), DataError(300), SchemaError(400), BusinessProcesssError(
125 500), UnknownError(900);
129 public int getValue() {
133 private ErrorCode(int value) {
138 private EELFLogger logger, auditLogger, metricsLogger;
139 private static final String CONFIG_FILE = System.getProperty("jboss.home.dir") + "/mso-config/uuid/uuid_"
140 + System.getProperty("jboss.server.name");
141 private static String instanceUUID, serverIP, serverName;
142 private MessageEnum exceptionArg, defaultException, defaultWarning, defaultAudit, defaultMetrics;
144 // For internal logging of the initialization of MSO logs
145 private static final Logger LOGGER = Logger.getLogger(MsoLogger.class.getName());
147 private MsoLogger(MsoLogger.Catalog cat) {
148 this.logger = EELFManager.getInstance().getErrorLogger();
149 this.auditLogger = EELFManager.getInstance().getAuditLogger();
150 this.metricsLogger = EELFManager.getInstance().getMetricsLogger();
151 MsoLogger.initialization();
152 setDefaultLogCatalog(cat);
155 private static synchronized void initialization() {
156 if (instanceUUID == null || ("").equals(instanceUUID)) {
157 instanceUUID = getInstanceUUID();
160 if (serverIP == null || serverName == null || ("").equals(serverIP) || ("").equals(serverName)) {
162 InetAddress server = InetAddress.getLocalHost();
163 serverIP = server.getHostAddress();
164 serverName = server.getHostName();
165 } catch (UnknownHostException e) {
166 LOGGER.log(Level.SEVERE, "Could not get local hostname", e);
174 * Get the MsoLogger based on the catalog
177 * Catalog of the logger
178 * @return the MsoLogger
180 public static synchronized MsoLogger getMsoLogger(MsoLogger.Catalog cat) {
181 return new MsoLogger(cat);
185 * Record the Metrics event with no argument
188 * Transaction starting time in millieseconds
190 * StatusCode of the transaction, either COMPLETE or ERROR
191 * @param responseCode
192 * The response code returned by the sub-components
193 * @param responseDesc
194 * Human redable description of the response code
195 * @param targetEntity
196 * The component which is invoked for this sub-operation
197 * @param targetServiceName
198 * API invoked on the TargetEntity
199 * @param targetVEntity
200 * Target VNF or VM acted opon by the component, if available
202 public void recordMetricEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode, String responseDesc,
203 String targetEntity, String targetServiceName, String targetVEntity) {
204 prepareMetricMsg(startTime, statusCode, responseCode.getValue(), responseDesc, targetEntity, targetServiceName,
206 metricsLogger.info("");
208 MDC.remove(TARGETENTITY);
209 MDC.remove(TARGETSERVICENAME);
213 * Record the Audit event
216 * Transaction starting time in millieseconds
218 * StatusCode of the transaction, either COMPLETE or ERROR
219 * @param responseCode
220 * The application specific response code
221 * @param responseDesc
222 * Human redable description of the application response code
224 public void recordAuditEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode,
225 String responseDesc) {
226 prepareAuditMsg(startTime, statusCode, responseCode.getValue(), responseDesc);
227 auditLogger.info("");
233 * Record the Debug event
236 * The log message to put
238 public void debug(String msg) {
239 prepareMsg(DEBUG_LEVEL);
244 * Record the Debug event
247 * The log message to put
249 * The exception to put
251 public void debug(String msg, Throwable t) {
252 prepareMsg(DEBUG_LEVEL);
253 logger.debug(msg, t);
258 * Record the Info event
261 * The log message to put
263 public void info(EELFResolvableErrorEnum msg, String targetEntity, String targetServiceName) {
264 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
267 MDC.remove(TARGETENTITY);
268 MDC.remove(TARGETSERVICENAME);
272 * Record the Info event with 1 argument
275 * The log message to put
277 * The argument used in the log message
279 public void info(EELFResolvableErrorEnum msg, String arg0, String targetEntity, String targetServiceName) {
280 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
282 logger.info(msg, normalize(arg0));
283 MDC.remove(TARGETENTITY);
284 MDC.remove(TARGETSERVICENAME);
288 * Record the Info event with 2 arguments
291 * The log message to put
293 * The arguments used in the log message
295 public void info(EELFResolvableErrorEnum msg, String arg0, String arg1, String targetEntity,
296 String targetServiceName) {
297 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
299 logger.info(msg, normalize(arg0), normalize(arg1));
300 MDC.remove(TARGETENTITY);
301 MDC.remove(TARGETSERVICENAME);
305 * Record the Info event with 3 arguments
308 * The log message to put
309 * @param arg0,arg1,arg2
310 * The arguments used in the log message
312 public void info(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String targetEntity,
313 String targetServiceName) {
314 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
316 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2));
317 MDC.remove(TARGETENTITY);
318 MDC.remove(TARGETSERVICENAME);
322 * Record the Info event with 4 arguments
325 * The log message to put
326 * @param arg0,arg1,arg2,arg3
327 * The arguments used in the log message
329 public void info(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3,
330 String targetEntity, String targetServiceName) {
331 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
333 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
334 MDC.remove(TARGETENTITY);
335 MDC.remove(TARGETSERVICENAME);
339 * Record the Info event with 5 arguments
342 * The log message to put
343 * @param arg0,arg1,arg2,arg3,arg4
344 * The arguments used in the log message
346 public void info(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
347 String targetEntity, String targetServiceName) {
348 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
350 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
351 MDC.remove(TARGETENTITY);
352 MDC.remove(TARGETSERVICENAME);
356 * Record the Info event with 6 arguments
359 * The log message to put
360 * @param arg0,arg1,arg2,arg3,arg4,arg5
361 * The arguments used in the log message
363 public void info(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
364 String arg5, String targetEntity, String targetServiceName) {
365 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
367 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4),
369 MDC.remove(TARGETENTITY);
370 MDC.remove(TARGETSERVICENAME);
375 * Record the Warning event
378 * The log message to put
380 public void warn(EELFResolvableErrorEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
382 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
385 MDC.remove(TARGETENTITY);
386 MDC.remove(TARGETSERVICENAME);
390 * Record the Warning event
393 * The log message to put
397 public void warn(EELFResolvableErrorEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
398 String errorDesc, Throwable t) {
399 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
401 logger.warn("Exception raised: " + getNormalizedStackTrace(t));
402 logger.debug("Exception raised", t);
403 MDC.remove(TARGETENTITY);
404 MDC.remove(TARGETSERVICENAME);
408 * Record the Warn event with 1 argument
411 * The log message to put
413 * The argument used in the log message
415 public void warn(EELFResolvableErrorEnum msg, String arg, String targetEntity, String targetServiceName,
416 ErrorCode errorCode, String errorDesc) {
417 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
418 logger.warn(msg, arg);
419 MDC.remove(TARGETENTITY);
420 MDC.remove(TARGETSERVICENAME);
424 * Record the Warn event with 1 argument
427 * The log message to put
429 * The arguments used in the log message
433 public void warn(EELFResolvableErrorEnum msg, String arg, String targetEntity, String targetServiceName,
434 ErrorCode errorCode, String errorDesc, Throwable t) {
435 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
436 logger.warn(msg, arg);
437 logger.warn("Exception raised: " + getNormalizedStackTrace(t));
438 logger.debug("Exception raised", t);
439 MDC.remove(TARGETENTITY);
440 MDC.remove(TARGETSERVICENAME);
444 * Record the Warn event with 2 arguments
447 * The log message to put
449 * The arguments used in the log message
451 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String targetEntity,
452 String targetServiceName, ErrorCode errorCode, String errorDesc) {
453 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
454 logger.warn(msg, normalize(arg0), normalize(arg1));
455 MDC.remove(TARGETENTITY);
456 MDC.remove(TARGETSERVICENAME);
460 * Record the Warn event with 2 arguments
463 * The log message to put
465 * The arguments used in the log message
469 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String targetEntity,
470 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
471 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
472 logger.warn(msg, normalize(arg0), normalize(arg1));
473 logger.warn("Exception raised: " + getNormalizedStackTrace(t));
474 logger.debug("Exception raised", t);
475 MDC.remove(TARGETENTITY);
476 MDC.remove(TARGETSERVICENAME);
480 * Record the Warn event with 3 arguments
483 * The log message to put
484 * @param arg0,arg1,arg2
485 * The arguments used in the log message
487 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String targetEntity,
488 String targetServiceName, ErrorCode errorCode, String errorDesc) {
489 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
490 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
491 MDC.remove(TARGETENTITY);
492 MDC.remove(TARGETSERVICENAME);
496 * Record the Warn event with 3 arguments
499 * The log message to put
500 * @param arg0,arg1,arg2
501 * The arguments used in the log message
505 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String targetEntity,
506 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
507 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
508 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
509 logger.warn("Exception raised: " + getNormalizedStackTrace(t));
510 logger.debug("Exception raised", t);
511 MDC.remove(TARGETENTITY);
512 MDC.remove(TARGETSERVICENAME);
516 * Record the Warn event with 4 arguments
519 * The log message to put
520 * @param arg0,arg1,arg2,arg3
521 * The arguments used in the log message
523 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3,
524 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
525 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
526 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
527 MDC.remove(TARGETENTITY);
528 MDC.remove(TARGETSERVICENAME);
532 * Record the Warn event with 4 arguments
535 * The log message to put
536 * @param arg0,arg1,arg2,
537 * arg3 The arguments used in the log message
541 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3,
542 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
543 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
544 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
545 logger.warn("Exception raised: " + getNormalizedStackTrace(t));
546 logger.debug("Exception raised", t);
547 MDC.remove(TARGETENTITY);
548 MDC.remove(TARGETSERVICENAME);
552 * Record the Warn event with 5 arguments
555 * The log message to put
556 * @param arg0,arg1,arg2,arg3,arg4
557 * The arguments used in the log message
559 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
560 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
561 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
562 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
563 MDC.remove(TARGETENTITY);
564 MDC.remove(TARGETSERVICENAME);
568 * Record the Warn event with 5 arguments
571 * The log message to put
572 * @param arg0,arg1,arg2,arg3,arg4
573 * The arguments used in the log message
577 public void warn(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
578 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
579 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
580 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
581 logger.warn("Exception raised: " + getNormalizedStackTrace(t));
582 logger.debug("Exception raised", t);
583 MDC.remove(TARGETENTITY);
584 MDC.remove(TARGETSERVICENAME);
589 * Record the Error event
592 * The log message to put
594 public void error(EELFResolvableErrorEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
596 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
598 MDC.remove(TARGETENTITY);
599 MDC.remove(TARGETSERVICENAME);
603 * Record the Error event
606 * The log message to put
610 public void error(EELFResolvableErrorEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
611 String errorDesc, Throwable t) {
612 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
614 logger.error(exceptionArg, getNormalizedStackTrace(t));
615 logger.debug("Exception raised", t);
616 MDC.remove(TARGETENTITY);
617 MDC.remove(TARGETSERVICENAME);
621 * Record the Error event with 1 argument
624 * The log message to put
626 * The arguments used in the log message
628 public void error(EELFResolvableErrorEnum msg, String arg0, String targetEntity, String targetServiceName,
629 ErrorCode errorCode, String errorDesc) {
630 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
631 logger.error(msg, normalize(arg0));
632 MDC.remove(TARGETENTITY);
633 MDC.remove(TARGETSERVICENAME);
637 * Record the Error event with 1 argument
640 * The log message to put
642 * The arguments used in the log message
646 public void error(EELFResolvableErrorEnum msg, String arg0, String targetEntity, String targetServiceName,
647 ErrorCode errorCode, String errorDesc, Throwable t) {
648 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
649 logger.error(msg, normalize(arg0));
650 logger.error(exceptionArg, getNormalizedStackTrace(t));
651 logger.debug("Exception raised", t);
652 MDC.remove(TARGETENTITY);
653 MDC.remove(TARGETSERVICENAME);
657 * Record the Error event with 2 arguments
660 * The log message to put
662 * The arguments used in the log message
664 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String targetEntity,
665 String targetServiceName, ErrorCode errorCode, String errorDesc) {
666 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
667 logger.error(msg, normalize(arg0), normalize(arg1));
668 MDC.remove(TARGETENTITY);
669 MDC.remove(TARGETSERVICENAME);
673 * Record the Error event with 2 arguments
676 * The log message to put
678 * The arguments used in the log message
682 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String targetEntity,
683 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
684 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
685 logger.error(msg, normalize(arg0), normalize(arg1));
686 logger.error(exceptionArg, getNormalizedStackTrace(t));
687 logger.debug("Exception raised", t);
688 MDC.remove(TARGETENTITY);
689 MDC.remove(TARGETSERVICENAME);
693 * Record the Error event with 3 arguments
696 * The log message to put
697 * @param arg0,arg1,arg2
698 * The arguments used in the log message
700 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String targetEntity,
701 String targetServiceName, ErrorCode errorCode, String errorDesc) {
702 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
703 logger.error(msg, normalize(arg0), normalize(arg1), normalize(arg2));
704 MDC.remove(TARGETENTITY);
705 MDC.remove(TARGETSERVICENAME);
709 * Record the Error event with 3 arguments
712 * The log message to put
713 * @param arg0,arg1,arg2
714 * The arguments used in the log message
718 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String targetEntity,
719 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
720 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
721 logger.error(msg, normalize(arg0), normalize(arg1), normalize(arg2));
722 logger.error(exceptionArg, getNormalizedStackTrace(t));
723 logger.debug("Exception raised", t);
724 MDC.remove(TARGETENTITY);
725 MDC.remove(TARGETSERVICENAME);
729 * Record the Error event with 4 arguments
732 * The log message to put
733 * @param arg0,arg1,arg2,arg3
734 * The arguments used in the log message
736 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3,
737 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
738 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
739 logger.error(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
740 MDC.remove(TARGETENTITY);
741 MDC.remove(TARGETSERVICENAME);
745 * Record the Error event with 4 arguments
748 * The log message to put
749 * @param arg0,arg1,arg2,arg3
750 * The arguments used in the log message
754 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3,
755 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
756 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
757 logger.error(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
758 logger.error(exceptionArg, getNormalizedStackTrace(t));
759 logger.debug("Exception raised", t);
760 MDC.remove(TARGETENTITY);
761 MDC.remove(TARGETSERVICENAME);
765 * Record the Error event with 5 arguments
768 * The log message to put
769 * @param arg0,arg1,arg2,arg3,arg4
770 * The arguments used in the log message
772 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
773 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
774 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
775 logger.error(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
776 MDC.remove(TARGETENTITY);
777 MDC.remove(TARGETSERVICENAME);
781 * Record the Error event with 5 arguments
784 * The log message to put
785 * @param arg0,arg1,arg2,arg3,arg4
786 * The arguments used in the log message
790 public void error(EELFResolvableErrorEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
791 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
792 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
793 logger.error(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
794 logger.error(exceptionArg, getNormalizedStackTrace(t));
795 logger.debug("Exception raised", t);
796 MDC.remove(TARGETENTITY);
797 MDC.remove(TARGETSERVICENAME);
800 public boolean isDebugEnabled() {
801 return logger.isDebugEnabled();
804 private void prepareMsg(String loggingLevel) {
805 prepareMsg(loggingLevel, null, null);
808 private void prepareMsg(String loggingLevel, String serviceNamep, String timer) {
809 String reqId = MDC.get(REQUEST_ID);
810 String svcId = MDC.get(SERVICE_INSTANCE_ID);
812 // Based on the discussion with Adrian,
813 // if these 2 parameters is not available, using dummy value "trace-#"
814 if (reqId == null || reqId.isEmpty()) {
815 MDC.put(REQUEST_ID, DUMMY_VALUE);
818 if (svcId == null || svcId.isEmpty()) {
819 MDC.put(SERVICE_INSTANCE_ID, DUMMY_VALUE);
823 MDC.put(TIMER, timer);
828 MDC.put(SERVICE_NAME, getFinalServiceName(serviceNamep));
829 MDC.put(ALERT_SEVERITY, getSeverityLevel(loggingLevel));
830 MDC.put(INSTANCE_UUID, instanceUUID);
831 MDC.put(SERVER_IP, serverIP);
832 MDC.put(FQDN, serverName);
835 private void prepareAuditMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc) {
836 long endTime = System.currentTimeMillis();
837 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
838 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
841 private void prepareAuditMetricMsg(long startTime, long endTime, StatusCode statusCode, int responseCode,
842 String responseDesc) {
843 Date startDate = new Date(startTime);
844 Date endDate = new Date(endTime);
845 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
847 MDC.put(BEGINTIME, String.valueOf(formatter.format(startDate)));
848 MDC.put(ENDTIME, String.valueOf(formatter.format(endDate)));
849 MDC.put(STATUSCODE, statusCode.name());
850 MDC.put(RESPONSECODE, String.valueOf(responseCode));
851 MDC.put(RESPONSEDESC, responseDesc);
854 private void prepareErrorMsg(String loggingLevel, String targetEntity, String targetServiceName,
855 ErrorCode errorCode, String errorDesc) {
856 MDC.put(ALERT_SEVERITY, getSeverityLevel(loggingLevel));
857 MDC.put(ERRORCODE, String.valueOf(errorCode));
858 MDC.put(ERRORDESC, errorDesc);
859 MDC.put(TARGETENTITY, targetEntity);
860 MDC.put(TARGETSERVICENAME, targetServiceName);
861 MDC.put(SERVICE_NAME, getFinalServiceName(getServiceName()));
864 private void prepareMetricMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc,
865 String targetEntity, String targetServiceName, String targetVEntity) {
866 long endTime = System.currentTimeMillis();
867 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
868 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
870 // Populate Metric log specific parameter
871 MDC.put(TARGETENTITY, targetEntity);
872 MDC.put(TARGETSERVICENAME, targetServiceName);
874 if (null != targetVEntity) {
875 MDC.put(TARGETVIRTUALENTITY, targetVEntity);
879 private String getSeverityLevel(String loggingLevel) {
881 // According to the Nagios alerting: 0=OK; 1=WARNING; 2=UNKOWN;
883 switch (loggingLevel) {
900 private String getFinalServiceName(String serviceNamep) {
901 // This step to set the serviceName should be put after the className is
903 // since the default serviceName is obtained during the method to get
906 // There's 3 ways to set the serviceName. The first method has the most
907 // priority to set the value.
908 // a) If the serviceName is set within the log method, this value will
910 // b) If serviceName is not set within the log method, the value defined
911 // in the MDC will be used
912 // c) If nothing is set specifically, then MsoLogger will assign a
913 // default(MSO.<method_name>) value to it
914 String serName = MDC.get(MsoLogger.SERVICE_NAME);
916 // Check if service name was already set as the method name by a
917 // previous call to this method.
918 String isMethodNameStr = MDC.get(MsoLogger.SERVICE_NAME_IS_METHOD_NAME);
919 boolean isMethodName = isMethodNameStr != null && isMethodNameStr.equals(Boolean.TRUE.toString());
920 if (serviceNamep != null) {
922 } else if (serName != null && !isMethodName) {
926 MDC.put(MsoLogger.SERVICE_NAME_IS_METHOD_NAME, Boolean.TRUE.toString());
928 StackTraceElement[] classArr = new Exception().getStackTrace();
929 if (classArr.length >= 6) {
932 limit = classArr.length;
934 for (int i = 1; i < limit; i++) {
935 String className = classArr[i].getClassName();
936 if (!className.equals(this.getClass().getName())) {
937 return classArr[i].getMethodName();
940 return classArr[0].getMethodName();
943 // Based on the discussion with Adrian, instanceUUID is used to identifiy
945 // it is generated during mso instance initialization period
946 // The same mso instnace will use the same instanceUUID value, even after
948 private static String getInstanceUUID() {
949 // Avoid creation during build and tests
950 if (System.getProperty("jboss.server.name") == null) {
951 return "Test UUID as JBoss not found";
953 File configFile = new File(CONFIG_FILE);
955 BufferedReader in = null;
956 BufferedWriter bw = null;
958 // Verify whether instanceUUID file exist,
959 // If yes, read the content; if not, generate the instanceUUID and
961 if (configFile.exists()) {
962 // read the content of the file
963 in = new BufferedReader(new FileReader(CONFIG_FILE));
964 if ((uuid = in.readLine()) == null) {
965 // the file is empty, regenerate the file
966 uuid = UUID.randomUUID().toString();
967 FileWriter fw = new FileWriter(configFile.getAbsoluteFile());
968 bw = new BufferedWriter(fw);
974 // file doesn't exist yet -> create the file and generate the
976 uuid = UUID.randomUUID().toString();
977 configFile.getParentFile().mkdirs();
978 configFile.createNewFile();
979 FileWriter fw = new FileWriter(configFile.getAbsoluteFile());
980 bw = new BufferedWriter(fw);
984 } catch (IOException e) {
985 LOGGER.log(Level.SEVERE, "Error trying to read UUID file", e);
994 } catch (IOException ex) {
995 LOGGER.log(Level.SEVERE, "Error trying to close UUID file", ex);
1002 * Set the requestId and serviceInstanceId
1007 * The serviceInstanceId
1009 public static void setLogContext(String reqId, String svcId) {
1010 if (null != reqId) {
1011 MDC.put(REQUEST_ID, reqId);
1014 if (null != svcId) {
1015 MDC.put(SERVICE_INSTANCE_ID, svcId);
1020 * Set the remoteIp and the basic HTTP Authentication user
1023 * The remote ip address
1025 * The basic http authencitation user
1027 public static void setLoggerParameters(String remoteIpp, String userp) {
1028 if (null != remoteIpp) {
1029 MDC.put(REMOTE_HOST, remoteIpp);
1031 if (null != userp) {
1032 MDC.put(USER, userp);
1037 * Set the serviceName
1039 * @param serviceNamep
1042 public static void setServiceName(String serviceNamep) {
1043 if (null != serviceNamep) {
1044 MDC.put(SERVICE_NAME, serviceNamep);
1045 MDC.remove(SERVICE_NAME_IS_METHOD_NAME);
1050 * Get the serviceName
1052 * @return The service name
1054 public static String getServiceName() {
1055 return MDC.get(SERVICE_NAME);
1059 * Reset the serviceName
1061 public static void resetServiceName() {
1062 MDC.remove(SERVICE_NAME);
1066 * Set the requestId and serviceInstanceId based on the mso request
1071 public static void setLogContext(MsoRequest msoRequest) {
1072 if (msoRequest != null) {
1073 MDC.put(REQUEST_ID, msoRequest.getRequestId());
1074 MDC.put(SERVICE_INSTANCE_ID, msoRequest.getServiceInstanceId());
1076 MDC.put(REQUEST_ID, DUMMY_VALUE);
1077 MDC.put(SERVICE_INSTANCE_ID, DUMMY_VALUE);
1081 private String normalize(String input) {
1082 if (input == null) {
1085 String result = input.replace('|', '!');
1086 result = result.replace("\n", " - ");
1090 private String getNormalizedStackTrace(Throwable t) {
1091 StringWriter sw = new StringWriter();
1092 PrintWriter pw = new PrintWriter(sw);
1093 t.printStackTrace(pw);
1094 return sw.toString().replace('|', '!').replace("\n", " - ");
1097 private void setDefaultLogCatalog(MsoLogger.Catalog cat) {
1098 if ("APIH".equals(cat.toString())) {
1099 exceptionArg = MessageEnum.APIH_GENERAL_EXCEPTION_ARG;
1100 defaultException = MessageEnum.APIH_GENERAL_EXCEPTION;
1101 defaultWarning = MessageEnum.APIH_GENERAL_WARNING;
1102 defaultAudit = MessageEnum.APIH_AUDIT_EXEC;
1103 defaultMetrics = MessageEnum.APIH_GENERAL_METRICS;
1104 } else if ("RA".equals(cat.toString())) {
1105 exceptionArg = MessageEnum.RA_GENERAL_EXCEPTION_ARG;
1106 defaultException = MessageEnum.RA_GENERAL_EXCEPTION;
1107 defaultWarning = MessageEnum.RA_GENERAL_WARNING;
1108 defaultAudit = MessageEnum.RA_AUDIT_EXEC;
1109 defaultMetrics = MessageEnum.RA_GENERAL_METRICS;
1110 } else if ("BPEL".equals(cat.toString())) {
1111 exceptionArg = MessageEnum.BPMN_GENERAL_EXCEPTION_ARG;
1112 defaultException = MessageEnum.BPMN_GENERAL_EXCEPTION;
1113 defaultWarning = MessageEnum.BPMN_GENERAL_WARNING;
1114 defaultAudit = MessageEnum.BPMN_AUDIT_EXEC;
1115 defaultMetrics = MessageEnum.BPMN_GENERAL_METRICS;
1116 } else if ("ASDC".equals(cat.toString())) {
1117 exceptionArg = MessageEnum.ASDC_GENERAL_EXCEPTION_ARG;
1118 defaultException = MessageEnum.ASDC_GENERAL_EXCEPTION;
1119 defaultWarning = MessageEnum.ASDC_GENERAL_WARNING;
1120 defaultAudit = MessageEnum.ASDC_AUDIT_EXEC;
1121 defaultMetrics = MessageEnum.ASDC_GENERAL_METRICS;
1123 exceptionArg = MessageEnum.GENERAL_EXCEPTION_ARG;
1124 defaultException = MessageEnum.GENERAL_EXCEPTION;
1125 defaultWarning = MessageEnum.GENERAL_WARNING;
1126 defaultAudit = MessageEnum.AUDIT_EXEC;
1127 defaultMetrics = MessageEnum.GENERAL_METRICS;