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.onap.so.logger;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.lang.invoke.MethodHandles;
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 import java.text.DateFormat;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.so.entity.MsoRequest;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import org.slf4j.Marker;
40 import org.slf4j.MarkerFactory;
44 * This class supports all of the normal logging functions (debug, info, etc.), prepending
45 * a string of format "[<requestId>|<serviceId]" to each message.
47 * SO code should initialize with these IDs when available, so that individual
48 * requests and/or services can be tracked throughout the various MSO component
49 * logs (API Handler, BPEL, and Adapters).
54 public class MsoLogger {
55 // Required MDC parameters
56 public static final String REQUEST_ID = "RequestID";
57 public static final String INVOCATION_ID = "InvocationID";
58 public static final String INSTANCE_UUID = "InstanceUUID";
59 public static final String SERVICE_NAME = "ServiceName";
60 public static final String STATUSCODE = "StatusCode";
61 public static final String RESPONSECODE = "ResponseCode";
62 public static final String RESPONSEDESC = "ResponseDesc";
63 public static final String FQDN = "ServerFQDN";
64 public static final String ENTRY_TIMESTAMP = "EntryTimestamp";
65 public static final String CLIENT_IPADDRESS = "EntryTimestamp";
69 public static final String HEADER_FROM_APP_ID = "X-FromAppId";
70 public static final String ONAP_PARTNER_NAME = "X-ONAP-PartnerName";
71 public static final String HEADER_REQUEST_ID = "X-RequestId";
72 public static final String TRANSACTION_ID = "X-TransactionID";
73 public static final String ECOMP_REQUEST_ID = "X-ECOMP-RequestID";
74 public static final String ONAP_REQUEST_ID = "X-ONAP-RequestID";
75 public static final String CLIENT_ID = "X-ClientID";
76 public static final String INVOCATION_ID_HEADER = "X-InvocationID";
77 public static final String REQUESTOR_ID = "X-RequestorID";
79 //Default values for not found
80 public static final String UNKNOWN_PARTNER = "UnknownPartner";
82 public static final String SERVICE_INSTANCE_ID = "ServiceInstanceId";
83 public static final String SERVICE_NAME_IS_METHOD_NAME = "ServiceNameIsMethodName";
84 public static final String SERVER_IP = "ServerIPAddress";
86 public static final String REMOTE_HOST = "RemoteHost";
87 public static final String ALERT_SEVERITY = "AlertSeverity";
88 public static final String TIMER = "Timer";
89 public static final String USER = "User";
90 public static final String DUMMY_VALUE = "trace-#";
91 public static final String UNKNOWN = "UNKNOWN";
92 public static final String CAT_LOG_LEVEL = "CategoryLogLevel";
93 public static final String AUDI_CAT_LOG_LEVEL = "AuditCategoryLogLevel";
97 public static final String PARTNER_NAME = "PartnerName";
99 // Audit/Metric log specific
100 public static final String BEGINTIME = "BeginTimestamp";
101 public static final String STARTTIME = "StartTimeMilis";
102 public static final String ENDTIME = "EndTimestamp";
103 public static final String PARTNERNAME = "PartnerName";
107 // Metric log specific
108 public static final String METRIC_BEGIN_TIME = "MetricBeginTime";
109 public static final String METRIC_START_TIME = "MetricStartTime";
110 public static final String METRIC_END_TIME = "MetricEndTime";
111 public static final String METRIC_TIMER = "MetricEndTime";
112 public static final String TARGETENTITY = "TargetEntity";
113 public static final String TARGETSERVICENAME = "TargetServiceName";
114 public static final String TARGETVIRTUALENTITY = "TargetVirtualEntity";
116 public static final String FATAL_LEVEL = "FATAL";
117 public static final String ERROR_LEVEL = "ERROR";
118 public static final String WARN_LEVEL = "WARN";
119 public static final String INFO_LEVEL = "INFO";
120 public static final String DEBUG_LEVEL = "DEBUG";
122 public static final String ERRORCODE = "ErrorCode";
123 public static final String ERRORDESC = "ErrorDesc";
126 public static final String COMPLETE = "COMPLETE";
127 public static final String INPROGRESS = "INPROGRESS";
129 public enum Catalog {
130 APIH, BPEL, RA, ASDC, GENERAL
133 public enum StatusCode {
137 public enum TargetEntity {
142 TargetEntity(String name) {
146 public String getUrl() {
151 public enum ResponseCode {
152 Suc(0), PermissionError(100), DataError(300), DataNotFound(301), BadRequest(302), SchemaError(
153 400), BusinessProcesssError(500), ServiceNotAvailable(501), InternalError(
154 502), Conflict(503), DBAccessError(504), CommunicationError(505), UnknownError(900);
158 public int getValue() {
162 private ResponseCode(int value) {
167 public enum ErrorCode {
168 PermissionError(100), AvailabilityError(200), DataError(300), SchemaError(400), BusinessProcesssError(
169 500), UnknownError(900);
173 public int getValue() {
177 private ErrorCode(int value) {
183 private Logger logger;
184 private Logger metricsLogger;
185 private Logger auditLogger;
186 private static String instanceUUID, serverIP, serverName;
187 private MessageEnum exceptionArg, defaultException, defaultWarning, defaultAudit, defaultMetrics;
189 private MsoLogger() {
190 this(MsoLogger.Catalog.GENERAL);
193 private MsoLogger(MsoLogger.Catalog cat) {
194 this(cat, MethodHandles.lookup().lookupClass());
197 private MsoLogger(MsoLogger.Catalog cat, Class<?> clazz) {
198 this.logger = LoggerFactory.getLogger(clazz);
199 this.auditLogger = LoggerFactory.getLogger("AUDIT");
200 this.metricsLogger = LoggerFactory.getLogger("METRIC");
201 setDefaultLogCatalog(cat);
204 public static MsoLogger getMsoLogger(MsoLogger.Catalog cat, Class<?> clazz) {
205 return new MsoLogger(cat,clazz);
211 * Record the Metrics event with no argument
214 * Transaction starting time in millieseconds
216 * StatusCode of the transaction, either COMPLETE or ERROR
217 * @param responseCode
218 * The response code returned by the sub-components
219 * @param responseDesc
220 * Human redable description of the response code
221 * @param targetEntity
222 * The component which is invoked for this sub-operation
223 * @param targetServiceName
224 * API invoked on the TargetEntity
225 * @param targetVEntity
226 * Target VNF or VM acted opon by the component, if available
229 public void recordMetricEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode, String responseDesc,
230 String targetEntity, String targetServiceName, String targetVEntity) {
231 prepareMetricMsg(startTime, statusCode, responseCode.getValue(), responseDesc, targetEntity, targetServiceName,
233 metricsLogger.info("");
235 MDC.remove(TARGETENTITY);
236 MDC.remove(TARGETSERVICENAME);
241 * Record the Audit event
244 * Transaction starting time in millieseconds
246 * StatusCode of the transaction, either COMPLETE or ERROR
247 * @param responseCode
248 * The application specific response code
249 * @param responseDesc
250 * Human redable description of the application response code
253 public void recordAuditEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode,
254 String responseDesc) {
255 if (StringUtils.isEmpty(MDC.get(MsoLogger.PARTNERNAME))) {
256 MDC.put(MsoLogger.PARTNERNAME, "UNKNOWN");
258 prepareAuditMsg(startTime, statusCode, responseCode.getValue(), responseDesc);
259 auditLogger.info("");
265 * Record the Debug event
268 * The log message to put
270 public void debug(String msg) {
271 prepareMsg(DEBUG_LEVEL);
276 * Record the Debug event
279 * The log message to put
281 * The exception to put
283 public void debug(String msg, Throwable t) {
284 prepareMsg(DEBUG_LEVEL);
285 logger.debug(msg, t);
288 public void info(String msg) {
289 prepareMsg(DEBUG_LEVEL);
295 * Log error message with the details of the exception that caused the error.
299 public void error(String msg) {
300 prepareMsg(ERROR_LEVEL);
305 * Log error message with the details of the exception that caused the error.
309 public void error(String msg, Throwable throwable) {
310 prepareMsg(ERROR_LEVEL);
311 logger.error(msg, throwable);
316 * Record the Info event
319 * The log message to put
321 public void info(String msg, String targetEntity, String targetServiceName) {
322 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
325 MDC.remove(TARGETENTITY);
326 MDC.remove(TARGETSERVICENAME);
330 * Record the Info event with 1 argument
333 * The log message to put
335 * The argument used in the log message
337 public void info(MessageEnum msg, String arg0, String targetEntity, String targetServiceName) {
338 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
340 logger.info(msg.toString(), normalize(arg0));
341 MDC.remove(TARGETENTITY);
342 MDC.remove(TARGETSERVICENAME);
346 * Record the Info event with 2 arguments
349 * The log message to put
351 * The arguments used in the log message
353 public void info(String msg, String arg0, String arg1, String targetEntity,
354 String targetServiceName) {
355 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
357 logger.info(msg, normalize(arg0), normalize(arg1));
358 MDC.remove(TARGETENTITY);
359 MDC.remove(TARGETSERVICENAME);
363 * Record the Info event with 3 arguments
366 * The log message to put
367 * @param arg0,arg1,arg2
368 * The arguments used in the log message
370 public void info(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
371 String targetServiceName) {
372 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
374 logger.info(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2));
375 MDC.remove(TARGETENTITY);
376 MDC.remove(TARGETSERVICENAME);
380 * Record the Info event with 4 arguments
383 * The log message to put
384 * @param arg0,arg1,arg2,arg3
385 * The arguments used in the log message
387 public void info(String msg, String arg0, String arg1, String arg2, String arg3,
388 String targetEntity, String targetServiceName) {
389 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
391 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
392 MDC.remove(TARGETENTITY);
393 MDC.remove(TARGETSERVICENAME);
397 * Record the Info event with 5 arguments
400 * The log message to put
401 * @param arg0,arg1,arg2,arg3,arg4
402 * The arguments used in the log message
404 public void info(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
405 String targetEntity, String targetServiceName) {
406 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
408 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
409 MDC.remove(TARGETENTITY);
410 MDC.remove(TARGETSERVICENAME);
414 * Record the Info event with 6 arguments
417 * The log message to put
418 * @param arg0,arg1,arg2,arg3,arg4,arg5
419 * The arguments used in the log message
421 public void info(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
422 String arg5, String targetEntity, String targetServiceName) {
423 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
425 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4),
427 MDC.remove(TARGETENTITY);
428 MDC.remove(TARGETSERVICENAME);
435 * Record the Warning event
438 * The log message to put
440 public void warnSimple( String targetServiceName, String errorDesc) {
441 logger.warn("Service Name: {} Error: {}" , targetServiceName, errorDesc);
444 * Record the Warning event
447 * The log message to put
449 public void warn(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
451 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
453 logger.warn(msg.toString());
454 MDC.remove(TARGETENTITY);
455 MDC.remove(TARGETSERVICENAME);
459 * Record the Warning event
462 * The log message to put
466 public void warn(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
467 String errorDesc, Throwable t) {
468 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
469 logger.warn("Warning: "+msg, t);
470 MDC.remove(TARGETENTITY);
471 MDC.remove(TARGETSERVICENAME);
475 * Record the Warn event with 1 argument
478 * The log message to put
480 * The argument used in the log message
482 public void warn(MessageEnum msg, String arg, String targetEntity, String targetServiceName,
483 ErrorCode errorCode, String errorDesc) {
484 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
485 logger.warn(msg.toString(), arg);
486 MDC.remove(TARGETENTITY);
487 MDC.remove(TARGETSERVICENAME);
491 * Record the Warn event with 1 argument
494 * The log message to put
496 * The arguments used in the log message
500 public void warn(MessageEnum msg, String arg, String targetEntity, String targetServiceName,
501 ErrorCode errorCode, String errorDesc, Throwable t) {
502 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
503 logger.warn(msg.toString(), arg);
504 logger.debug("Exception raised", t);
505 MDC.remove(TARGETENTITY);
506 MDC.remove(TARGETSERVICENAME);
510 * Record the Warn event with 2 arguments
513 * The log message to put
515 * The arguments used in the log message
517 public void warn(MessageEnum msg, String arg0, String arg1, String targetEntity,
518 String targetServiceName, ErrorCode errorCode, String errorDesc) {
519 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
520 logger.warn(msg.toString(), normalize(arg0), normalize(arg1));
521 MDC.remove(TARGETENTITY);
522 MDC.remove(TARGETSERVICENAME);
526 * Record the Warn event with 2 arguments
529 * The log message to put
531 * The arguments used in the log message
535 public void warn(String msg, String arg0, String arg1, String targetEntity,
536 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
537 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
538 logger.warn(msg, normalize(arg0), normalize(arg1));
540 MDC.remove(TARGETENTITY);
541 MDC.remove(TARGETSERVICENAME);
545 * Record the Warn event with 3 arguments
548 * The log message to put
549 * @param arg0,arg1,arg2
550 * The arguments used in the log message
552 public void warn(String msg, String arg0, String arg1, String arg2, String targetEntity,
553 String targetServiceName, ErrorCode errorCode, String errorDesc) {
554 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
555 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
556 MDC.remove(TARGETENTITY);
557 MDC.remove(TARGETSERVICENAME);
561 * Record the Warn event with 3 arguments
564 * The log message to put
565 * @param arg0,arg1,arg2
566 * The arguments used in the log message
570 public void warn(String msg, String arg0, String arg1, String arg2, String targetEntity,
571 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
572 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
573 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
575 MDC.remove(TARGETENTITY);
576 MDC.remove(TARGETSERVICENAME);
580 * Record the Warn event with 4 arguments
583 * The log message to put
584 * @param arg0,arg1,arg2,arg3
585 * The arguments used in the log message
587 public void warn(String msg, String arg0, String arg1, String arg2, String arg3,
588 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
589 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
590 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
591 MDC.remove(TARGETENTITY);
592 MDC.remove(TARGETSERVICENAME);
596 * Record the Warn event with 4 arguments
599 * The log message to put
600 * @param arg0,arg1,arg2,
601 * arg3 The arguments used in the log message
605 public void warn(String msg, String arg0, String arg1, String arg2, String arg3,
606 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
607 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
608 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
610 MDC.remove(TARGETENTITY);
611 MDC.remove(TARGETSERVICENAME);
615 * Record the Warn event with 5 arguments
618 * The log message to put
619 * @param arg0,arg1,arg2,arg3,arg4
620 * The arguments used in the log message
622 public void warn(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
623 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
624 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
625 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
626 MDC.remove(TARGETENTITY);
627 MDC.remove(TARGETSERVICENAME);
631 * Record the Warn event with 5 arguments
634 * The log message to put
635 * @param arg0,arg1,arg2,arg3,arg4
636 * The arguments used in the log message
640 public void warn(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
641 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
642 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
643 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
645 MDC.remove(TARGETENTITY);
646 MDC.remove(TARGETSERVICENAME);
653 * Record the Error event
655 * @param generalException
656 * The log message to put
658 public void error(MessageEnum generalException, String targetEntity, String targetServiceName, ErrorCode errorCode,
660 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
661 logger.error(generalException.toString() + ": " +errorDesc);
662 MDC.remove(TARGETENTITY);
663 MDC.remove(TARGETSERVICENAME);
668 * Record the Error event
671 * The log message to put
675 public void trace(String traceMessage) {
676 logger.trace(traceMessage);
681 * Record the Error event
684 * The log message to put
688 public void error( Throwable t) {
689 logger.error(t.getMessage(), t);
694 * Record the Error event
697 * The log message to put
701 public void error(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
702 String errorDesc, Throwable t) {
703 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
704 logger.error(msg.toString(), t);
705 MDC.remove(TARGETENTITY);
706 MDC.remove(TARGETSERVICENAME);
710 * Record the Error event with 1 argument
713 * The log message to put
715 * The arguments used in the log message
717 public void error(MessageEnum msg, String arg0, String targetEntity, String targetServiceName,
718 ErrorCode errorCode, String errorDesc) {
719 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
720 logger.error(msg.toString(), normalize(arg0));
721 MDC.remove(TARGETENTITY);
722 MDC.remove(TARGETSERVICENAME);
726 * Record the Error event with 1 argument
729 * The log message to put
731 * The arguments used in the log message
735 public void error(MessageEnum msg, String arg0, String targetEntity, String targetServiceName,
736 ErrorCode errorCode, String errorDesc, Throwable t) {
737 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
738 logger.error(msg.toString(), normalize(arg0), t);
739 MDC.remove(TARGETENTITY);
740 MDC.remove(TARGETSERVICENAME);
744 * Record the Error event with 2 arguments
747 * The log message to put
749 * The arguments used in the log message
751 public void error(MessageEnum msg, String arg0, String arg1, String targetEntity,
752 String targetServiceName, ErrorCode errorCode, String errorDesc) {
753 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
754 logger.error(msg.toString(), normalize(arg0), normalize(arg1));
755 MDC.remove(TARGETENTITY);
756 MDC.remove(TARGETSERVICENAME);
760 * Record the Error event with 2 arguments
763 * The log message to put
765 * The arguments used in the log message
769 public void error(MessageEnum msg, String arg0, String arg1, String targetEntity,
770 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
771 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
772 logger.error(msg.toString(), normalize(arg0), normalize(arg1), t);
773 logger.debug("Exception raised", t);
774 MDC.remove(TARGETENTITY);
775 MDC.remove(TARGETSERVICENAME);
779 * Record the Error event with 3 arguments
782 * The log message to put
783 * @param arg0,arg1,arg2
784 * The arguments used in the log message
786 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
787 String targetServiceName, ErrorCode errorCode, String errorDesc) {
788 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
789 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2));
790 MDC.remove(TARGETENTITY);
791 MDC.remove(TARGETSERVICENAME);
795 * Record the Error event with 3 arguments
798 * The log message to put
799 * @param arg0,arg1,arg2
800 * The arguments used in the log message
804 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
805 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
806 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
807 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), t);
808 MDC.remove(TARGETENTITY);
809 MDC.remove(TARGETSERVICENAME);
813 * Record the Error event with 4 arguments
816 * The log message to put
817 * @param arg0,arg1,arg2,arg3
818 * The arguments used in the log message
820 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3,
821 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
822 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
823 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
824 MDC.remove(TARGETENTITY);
825 MDC.remove(TARGETSERVICENAME);
829 * Record the Error event with 4 arguments
832 * The log message to put
833 * @param arg0,arg1,arg2,arg3
834 * The arguments used in the log message
838 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3,
839 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
840 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
841 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), t);
843 logger.debug("Exception raised", t);
844 MDC.remove(TARGETENTITY);
845 MDC.remove(TARGETSERVICENAME);
849 * Record the Error event with 5 arguments
852 * The log message to put
853 * @param arg0,arg1,arg2,arg3,arg4
854 * The arguments used in the log message
856 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
857 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
858 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
859 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
860 MDC.remove(TARGETENTITY);
861 MDC.remove(TARGETSERVICENAME);
865 * Record the Error event with 5 arguments
868 * The log message to put
869 * @param arg0,arg1,arg2,arg3,arg4
870 * The arguments used in the log message
874 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
875 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
876 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
877 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4), t);
878 logger.debug("Exception raised", t);
879 MDC.remove(TARGETENTITY);
880 MDC.remove(TARGETSERVICENAME);
883 public void error(String errorMessage, String errorSource, String targetEntity, String targetServiceName,
884 ErrorCode errorCode, String errorText) {
885 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorText);
886 logger.error(errorMessage);
887 MDC.remove(TARGETENTITY);
888 MDC.remove(TARGETSERVICENAME);
892 private void prepareMsg(String loggingLevel) {
893 prepareMsg(loggingLevel, null, null);
896 private void prepareMsg(String loggingLevel, String serviceNamep, String timer) {
897 String reqId = MDC.get(REQUEST_ID);
898 String svcId = MDC.get(SERVICE_INSTANCE_ID);
900 // Based on the discussion with Adrian,
901 // if these 2 parameters is not available, using dummy value "trace-#"
902 if (reqId == null || reqId.isEmpty()) {
903 MDC.put(REQUEST_ID, DUMMY_VALUE);
907 MDC.put(TIMER, timer);
910 writeIfNotNullorEmpty(SERVICE_NAME,getFinalServiceName(serviceNamep));
911 writeIfNotNullorEmpty(ALERT_SEVERITY,getSeverityLevel(loggingLevel));
912 writeIfNotNullorEmpty(INSTANCE_UUID,instanceUUID);
913 writeIfNotNullorEmpty(SERVER_IP,serverIP);
914 writeIfNotNullorEmpty(FQDN,serverName);
918 private void writeIfNotNullorEmpty(String Key, String value) {
919 if (MDC.get(Key) == null|| MDC.get(Key).isEmpty()) {
924 private void prepareAuditMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc) {
925 long endTime = System.currentTimeMillis();
926 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
927 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
930 private void prepareAuditMetricMsg(long startTime, long endTime, StatusCode statusCode, int responseCode,
931 String responseDesc) {
932 Date startDate = new Date(startTime);
933 Date endDate = new Date(endTime);
934 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
936 MDC.put(BEGINTIME, String.valueOf(formatter.format(startDate)));
937 MDC.put(ENDTIME, String.valueOf(formatter.format(endDate)));
938 MDC.put(STATUSCODE, statusCode.name());
939 MDC.put(RESPONSECODE, String.valueOf(responseCode));
940 MDC.put(RESPONSEDESC, responseDesc);
943 private void prepareErrorMsg(String loggingLevel, String targetEntity, String targetServiceName,
944 ErrorCode errorCode, String errorDesc) {
945 MDC.put(ALERT_SEVERITY, getSeverityLevel(loggingLevel));
946 if(errorCode != null) {
947 MDC.put(ERRORCODE, Integer.toString(errorCode.getValue()));
949 MDC.put(ERRORDESC, errorDesc);
950 MDC.put(TARGETENTITY, targetEntity);
951 MDC.put(TARGETSERVICENAME, targetServiceName);
954 private void prepareMetricMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc,
955 String targetEntity, String targetServiceName, String targetVEntity) {
956 long endTime = System.currentTimeMillis();
957 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
958 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
960 // Populate Metric log specific parameter
961 MDC.put(TARGETENTITY, targetEntity);
962 MDC.put(TARGETSERVICENAME, targetServiceName);
964 if (null != targetVEntity) {
965 MDC.put(TARGETVIRTUALENTITY, targetVEntity);
969 private String getSeverityLevel(String loggingLevel) {
971 // According to the Nagios alerting: 0=OK; 1=WARNING; 2=UNKOWN;
973 switch (loggingLevel) {
990 private String getFinalServiceName(String serviceNamep) {
991 // This step to set the serviceName should be put after the className is
993 // since the default serviceName is obtained during the method to get
996 // There's 3 ways to set the serviceName. The first method has the most
997 // priority to set the value.
998 // a) If the serviceName is set within the log method, this value will
1000 // b) If serviceName is not set within the log method, the value defined
1001 // in the MDC will be used
1002 // c) If nothing is set specifically, then MsoLogger will assign a
1003 // default(MSO.<method_name>) value to it
1004 String serName = MDC.get(MsoLogger.SERVICE_NAME);
1006 // Check if service name was already set as the method name by a
1007 // previous call to this method.
1008 String isMethodNameStr = MDC.get(MsoLogger.SERVICE_NAME_IS_METHOD_NAME);
1009 boolean isMethodName = isMethodNameStr != null && isMethodNameStr.equals(Boolean.TRUE.toString());
1010 if (serviceNamep != null) {
1011 return serviceNamep;
1012 } else if (serName != null && !isMethodName) {
1016 MDC.put(MsoLogger.SERVICE_NAME_IS_METHOD_NAME, Boolean.TRUE.toString());
1018 StackTraceElement[] classArr = new Exception().getStackTrace();
1019 if (classArr.length >= 6) {
1022 limit = classArr.length;
1024 for (int i = 1; i < limit; i++) {
1025 if (!classArr[i].getClassName().equals(this.getClass().getName())) {
1026 return classArr[i].getMethodName();
1029 return classArr[0].getMethodName();
1033 * Set the requestId and serviceInstanceId
1038 * The serviceInstanceId
1040 public static void setLogContext(String reqId, String svcId) {
1041 if (null != reqId) {
1042 MDC.put(REQUEST_ID, reqId);
1045 if (null != svcId) {
1046 MDC.put(SERVICE_INSTANCE_ID, svcId);
1051 * Get the serviceName
1053 * @return The service name
1055 public static String getServiceName() {
1056 return MDC.get(SERVICE_NAME);
1059 private String normalize(String input) {
1060 if (input == null) {
1063 String result = input.replace('|', '!');
1064 result = result.replace("\n", " - ");
1068 private void setDefaultLogCatalog(MsoLogger.Catalog cat) {
1069 if ("APIH".equals(cat.toString())) {
1070 exceptionArg = MessageEnum.APIH_GENERAL_EXCEPTION_ARG;
1071 defaultException = MessageEnum.APIH_GENERAL_EXCEPTION;
1072 defaultWarning = MessageEnum.APIH_GENERAL_WARNING;
1073 defaultAudit = MessageEnum.APIH_AUDIT_EXEC;
1074 defaultMetrics = MessageEnum.APIH_GENERAL_METRICS;
1075 } else if ("RA".equals(cat.toString())) {
1076 exceptionArg = MessageEnum.RA_GENERAL_EXCEPTION_ARG;
1077 defaultException = MessageEnum.RA_GENERAL_EXCEPTION;
1078 defaultWarning = MessageEnum.RA_GENERAL_WARNING;
1079 defaultAudit = MessageEnum.RA_AUDIT_EXEC;
1080 defaultMetrics = MessageEnum.RA_GENERAL_METRICS;
1081 } else if ("BPEL".equals(cat.toString())) {
1082 exceptionArg = MessageEnum.BPMN_GENERAL_EXCEPTION_ARG;
1083 defaultException = MessageEnum.BPMN_GENERAL_EXCEPTION;
1084 defaultWarning = MessageEnum.BPMN_GENERAL_WARNING;
1085 defaultAudit = MessageEnum.BPMN_AUDIT_EXEC;
1086 defaultMetrics = MessageEnum.BPMN_GENERAL_METRICS;
1087 } else if ("ASDC".equals(cat.toString())) {
1088 exceptionArg = MessageEnum.ASDC_GENERAL_EXCEPTION_ARG;
1089 defaultException = MessageEnum.ASDC_GENERAL_EXCEPTION;
1090 defaultWarning = MessageEnum.ASDC_GENERAL_WARNING;
1091 defaultAudit = MessageEnum.ASDC_AUDIT_EXEC;
1092 defaultMetrics = MessageEnum.ASDC_GENERAL_METRICS;
1094 exceptionArg = MessageEnum.GENERAL_EXCEPTION_ARG;
1095 defaultException = MessageEnum.GENERAL_EXCEPTION;
1096 defaultWarning = MessageEnum.GENERAL_WARNING;
1097 defaultAudit = MessageEnum.AUDIT_EXEC;
1098 defaultMetrics = MessageEnum.GENERAL_METRICS;