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";
78 //Default values for not found
79 public static final String UNKNOWN_PARTNER = "UnknownPartner";
81 public static final String SERVICE_INSTANCE_ID = "ServiceInstanceId";
82 public static final String SERVICE_NAME_IS_METHOD_NAME = "ServiceNameIsMethodName";
83 public static final String SERVER_IP = "ServerIPAddress";
85 public static final String REMOTE_HOST = "RemoteHost";
86 public static final String ALERT_SEVERITY = "AlertSeverity";
87 public static final String TIMER = "Timer";
88 public static final String USER = "User";
89 public static final String DUMMY_VALUE = "trace-#";
90 public static final String UNKNOWN = "UNKNOWN";
91 public static final String CAT_LOG_LEVEL = "CategoryLogLevel";
92 public static final String AUDI_CAT_LOG_LEVEL = "AuditCategoryLogLevel";
96 public static final String PARTNER_NAME = "PartnerName";
104 // Audit/Metric log specific
105 public static final String BEGINTIME = "BeginTimestamp";
106 public static final String STARTTIME = "StartTimeMilis";
107 public static final String ENDTIME = "EndTimestamp";
108 public static final String PARTNERNAME = "PartnerName";
112 // Metric log specific
113 public static final String METRIC_BEGIN_TIME = "MetricBeginTime";
114 public static final String METRIC_START_TIME = "MetricStartTime";
115 public static final String METRIC_END_TIME = "MetricEndTime";
116 public static final String METRIC_TIMER = "MetricEndTime";
117 public static final String TARGETENTITY = "TargetEntity";
118 public static final String TARGETSERVICENAME = "TargetServiceName";
119 public static final String TARGETVIRTUALENTITY = "TargetVirtualEntity";
121 public static final String FATAL_LEVEL = "FATAL";
122 public static final String ERROR_LEVEL = "ERROR";
123 public static final String WARN_LEVEL = "WARN";
124 public static final String INFO_LEVEL = "INFO";
125 public static final String DEBUG_LEVEL = "DEBUG";
127 public static final String ERRORCODE = "ErrorCode";
128 public static final String ERRORDESC = "ErrorDesc";
131 public static final String COMPLETE = "COMPLETE";
132 public static final String INPROGRESS = "INPROGRESS";
134 public enum Catalog {
135 APIH, BPEL, RA, ASDC, GENERAL
138 public enum StatusCode {
142 public enum TargetEntity {
147 TargetEntity(String name) {
151 public String getUrl() {
156 public enum ResponseCode {
157 Suc(0), PermissionError(100), DataError(300), DataNotFound(301), BadRequest(302), SchemaError(
158 400), BusinessProcesssError(500), ServiceNotAvailable(501), InternalError(
159 502), Conflict(503), DBAccessError(504), CommunicationError(505), UnknownError(900);
163 public int getValue() {
167 private ResponseCode(int value) {
172 public enum ErrorCode {
173 PermissionError(100), AvailabilityError(200), DataError(300), SchemaError(400), BusinessProcesssError(
174 500), UnknownError(900);
178 public int getValue() {
182 private ErrorCode(int value) {
187 public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
188 public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
190 private Logger logger;
191 private Logger metricsLogger;
192 private Logger auditLogger;
193 private static String instanceUUID, serverIP, serverName;
194 private MessageEnum exceptionArg, defaultException, defaultWarning, defaultAudit, defaultMetrics;
196 // For internal logging of the initialization of MSO logs
197 private static final Logger initLOGGER = LoggerFactory.getLogger(MsoLogger.class.getName());
199 private MsoLogger() {
200 this(MsoLogger.Catalog.GENERAL);
203 private MsoLogger(MsoLogger.Catalog cat) {
204 this(cat, MethodHandles.lookup().lookupClass());
207 private MsoLogger(MsoLogger.Catalog cat, Class<?> clazz) {
208 this.logger = LoggerFactory.getLogger(clazz);
209 this.auditLogger = LoggerFactory.getLogger("AUDIT");
210 this.metricsLogger = LoggerFactory.getLogger("METRIC");
211 MsoLogger.initialization();
212 setDefaultLogCatalog(cat);
215 private static synchronized void initialization() {
216 if (instanceUUID == null || ("").equals(instanceUUID)) {
217 instanceUUID = getInstanceUUID();
220 if (serverIP == null || serverName == null || ("").equals(serverIP) || ("").equals(serverName)) {
222 InetAddress server = InetAddress.getLocalHost();
223 serverIP = server.getHostAddress();
224 serverName = server.getHostName();
225 } catch (UnknownHostException e) {
226 initLOGGER.error("Could not get local hostname", e);
235 public static MsoLogger getMsoLogger(MsoLogger.Catalog cat, Class<?> clazz) {
236 return new MsoLogger(cat,clazz);
242 * Record the Metrics event with no argument
245 * Transaction starting time in millieseconds
247 * StatusCode of the transaction, either COMPLETE or ERROR
248 * @param responseCode
249 * The response code returned by the sub-components
250 * @param responseDesc
251 * Human redable description of the response code
252 * @param targetEntity
253 * The component which is invoked for this sub-operation
254 * @param targetServiceName
255 * API invoked on the TargetEntity
256 * @param targetVEntity
257 * Target VNF or VM acted opon by the component, if available
259 public void recordMetricEvent() {
260 metricsLogger.info("");
262 MDC.remove(TARGETENTITY);
263 MDC.remove(TARGETSERVICENAME);
266 public void recordMetricEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode, String responseDesc,
267 String targetEntity, String targetServiceName, String targetVEntity) {
268 prepareMetricMsg(startTime, statusCode, responseCode.getValue(), responseDesc, targetEntity, targetServiceName,
270 metricsLogger.info("");
272 MDC.remove(TARGETENTITY);
273 MDC.remove(TARGETSERVICENAME);
278 * Record the Audit event
281 * Transaction starting time in millieseconds
283 * StatusCode of the transaction, either COMPLETE or ERROR
284 * @param responseCode
285 * The application specific response code
286 * @param responseDesc
287 * Human redable description of the application response code
289 public void recordAuditEvent(){
290 auditLogger.info("");
293 public void recordAuditEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode,
294 String responseDesc) {
295 if (StringUtils.isEmpty(MDC.get(MsoLogger.PARTNERNAME))) {
296 MDC.put(MsoLogger.PARTNERNAME, "UNKNOWN");
298 prepareAuditMsg(startTime, statusCode, responseCode.getValue(), responseDesc);
299 auditLogger.info("");
305 * Record the Debug event
308 * The log message to put
310 public void debug(String msg) {
311 prepareMsg(DEBUG_LEVEL);
316 * Record the Debug event
319 * The log message to put
321 * The exception to put
323 public void debug(String msg, Throwable t) {
324 prepareMsg(DEBUG_LEVEL);
325 logger.debug(msg, t);
328 public void info(String msg) {
329 prepareMsg(DEBUG_LEVEL);
335 * Log error message with the details of the exception that caused the error.
339 public void error(String msg) {
340 prepareMsg(ERROR_LEVEL);
345 * Log error message with the details of the exception that caused the error.
349 public void error(String msg, Throwable throwable) {
350 prepareMsg(ERROR_LEVEL);
351 logger.error(msg, throwable);
356 * Record the Info event
359 * The log message to put
361 public void info(String msg, String targetEntity, String targetServiceName) {
362 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
365 MDC.remove(TARGETENTITY);
366 MDC.remove(TARGETSERVICENAME);
370 * Record the Info event with 1 argument
373 * The log message to put
375 * The argument used in the log message
377 public void info(MessageEnum msg, String arg0, String targetEntity, String targetServiceName) {
378 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
380 logger.info(msg.toString(), normalize(arg0));
381 MDC.remove(TARGETENTITY);
382 MDC.remove(TARGETSERVICENAME);
386 * Record the Info event with 2 arguments
389 * The log message to put
391 * The arguments used in the log message
393 public void info(String msg, String arg0, String arg1, String targetEntity,
394 String targetServiceName) {
395 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
397 logger.info(msg, normalize(arg0), normalize(arg1));
398 MDC.remove(TARGETENTITY);
399 MDC.remove(TARGETSERVICENAME);
403 * Record the Info event with 3 arguments
406 * The log message to put
407 * @param arg0,arg1,arg2
408 * The arguments used in the log message
410 public void info(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
411 String targetServiceName) {
412 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
414 logger.info(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2));
415 MDC.remove(TARGETENTITY);
416 MDC.remove(TARGETSERVICENAME);
420 * Record the Info event with 4 arguments
423 * The log message to put
424 * @param arg0,arg1,arg2,arg3
425 * The arguments used in the log message
427 public void info(String msg, String arg0, String arg1, String arg2, String arg3,
428 String targetEntity, String targetServiceName) {
429 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
431 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
432 MDC.remove(TARGETENTITY);
433 MDC.remove(TARGETSERVICENAME);
437 * Record the Info event with 5 arguments
440 * The log message to put
441 * @param arg0,arg1,arg2,arg3,arg4
442 * The arguments used in the log message
444 public void info(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
445 String targetEntity, String targetServiceName) {
446 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
448 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
449 MDC.remove(TARGETENTITY);
450 MDC.remove(TARGETSERVICENAME);
454 * Record the Info event with 6 arguments
457 * The log message to put
458 * @param arg0,arg1,arg2,arg3,arg4,arg5
459 * The arguments used in the log message
461 public void info(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
462 String arg5, String targetEntity, String targetServiceName) {
463 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
465 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4),
467 MDC.remove(TARGETENTITY);
468 MDC.remove(TARGETSERVICENAME);
475 * Record the Warning event
478 * The log message to put
480 public void warnSimple( String targetServiceName, String errorDesc) {
481 logger.warn("Service Name: {} Error: {}" , targetServiceName, errorDesc);
484 * Record the Warning event
487 * The log message to put
489 public void warn(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
491 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
493 logger.warn(msg.toString());
494 MDC.remove(TARGETENTITY);
495 MDC.remove(TARGETSERVICENAME);
499 * Record the Warning event
502 * The log message to put
506 public void warn(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
507 String errorDesc, Throwable t) {
508 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
509 logger.warn("Warning: "+msg, t);
510 MDC.remove(TARGETENTITY);
511 MDC.remove(TARGETSERVICENAME);
515 * Record the Warn event with 1 argument
518 * The log message to put
520 * The argument used in the log message
522 public void warn(MessageEnum msg, String arg, String targetEntity, String targetServiceName,
523 ErrorCode errorCode, String errorDesc) {
524 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
525 logger.warn(msg.toString(), arg);
526 MDC.remove(TARGETENTITY);
527 MDC.remove(TARGETSERVICENAME);
531 * Record the Warn event with 1 argument
534 * The log message to put
536 * The arguments used in the log message
540 public void warn(MessageEnum msg, String arg, String targetEntity, String targetServiceName,
541 ErrorCode errorCode, String errorDesc, Throwable t) {
542 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
543 logger.warn(msg.toString(), arg);
544 logger.debug("Exception raised", t);
545 MDC.remove(TARGETENTITY);
546 MDC.remove(TARGETSERVICENAME);
550 * Record the Warn event with 2 arguments
553 * The log message to put
555 * The arguments used in the log message
557 public void warn(MessageEnum msg, String arg0, String arg1, String targetEntity,
558 String targetServiceName, ErrorCode errorCode, String errorDesc) {
559 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
560 logger.warn(msg.toString(), normalize(arg0), normalize(arg1));
561 MDC.remove(TARGETENTITY);
562 MDC.remove(TARGETSERVICENAME);
566 * Record the Warn event with 2 arguments
569 * The log message to put
571 * The arguments used in the log message
575 public void warn(String msg, String arg0, String arg1, String targetEntity,
576 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
577 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
578 logger.warn(msg, normalize(arg0), normalize(arg1));
580 MDC.remove(TARGETENTITY);
581 MDC.remove(TARGETSERVICENAME);
585 * Record the Warn event with 3 arguments
588 * The log message to put
589 * @param arg0,arg1,arg2
590 * The arguments used in the log message
592 public void warn(String msg, String arg0, String arg1, String arg2, String targetEntity,
593 String targetServiceName, ErrorCode errorCode, String errorDesc) {
594 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
595 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
596 MDC.remove(TARGETENTITY);
597 MDC.remove(TARGETSERVICENAME);
601 * Record the Warn event with 3 arguments
604 * The log message to put
605 * @param arg0,arg1,arg2
606 * The arguments used in the log message
610 public void warn(String msg, String arg0, String arg1, String arg2, String targetEntity,
611 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
612 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
613 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
615 MDC.remove(TARGETENTITY);
616 MDC.remove(TARGETSERVICENAME);
620 * Record the Warn event with 4 arguments
623 * The log message to put
624 * @param arg0,arg1,arg2,arg3
625 * The arguments used in the log message
627 public void warn(String msg, String arg0, String arg1, String arg2, String arg3,
628 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
629 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
630 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
631 MDC.remove(TARGETENTITY);
632 MDC.remove(TARGETSERVICENAME);
636 * Record the Warn event with 4 arguments
639 * The log message to put
640 * @param arg0,arg1,arg2,
641 * arg3 The arguments used in the log message
645 public void warn(String msg, String arg0, String arg1, String arg2, String arg3,
646 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
647 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
648 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
650 MDC.remove(TARGETENTITY);
651 MDC.remove(TARGETSERVICENAME);
655 * Record the Warn event with 5 arguments
658 * The log message to put
659 * @param arg0,arg1,arg2,arg3,arg4
660 * The arguments used in the log message
662 public void warn(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
663 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
664 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
665 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
666 MDC.remove(TARGETENTITY);
667 MDC.remove(TARGETSERVICENAME);
671 * Record the Warn event with 5 arguments
674 * The log message to put
675 * @param arg0,arg1,arg2,arg3,arg4
676 * The arguments used in the log message
680 public void warn(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
681 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
682 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
683 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
685 MDC.remove(TARGETENTITY);
686 MDC.remove(TARGETSERVICENAME);
693 * Record the Error event
695 * @param generalException
696 * The log message to put
698 public void error(MessageEnum generalException, String targetEntity, String targetServiceName, ErrorCode errorCode,
700 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
701 logger.error(generalException.toString() + ": " +errorDesc);
702 MDC.remove(TARGETENTITY);
703 MDC.remove(TARGETSERVICENAME);
708 * Record the Error event
711 * The log message to put
715 public void trace(String traceMessage) {
716 logger.trace(traceMessage);
721 * Record the Error event
724 * The log message to put
728 public void error( Throwable t) {
729 logger.error(t.getMessage(), t);
734 * Record the Error event
737 * The log message to put
741 public void error(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
742 String errorDesc, Throwable t) {
743 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
744 logger.error(msg.toString(), t);
745 MDC.remove(TARGETENTITY);
746 MDC.remove(TARGETSERVICENAME);
750 * Record the Error event with 1 argument
753 * The log message to put
755 * The arguments used in the log message
757 public void error(MessageEnum msg, String arg0, String targetEntity, String targetServiceName,
758 ErrorCode errorCode, String errorDesc) {
759 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
760 logger.error(msg.toString(), normalize(arg0));
761 MDC.remove(TARGETENTITY);
762 MDC.remove(TARGETSERVICENAME);
766 * Record the Error event with 1 argument
769 * The log message to put
771 * The arguments used in the log message
775 public void error(MessageEnum msg, String arg0, String targetEntity, String targetServiceName,
776 ErrorCode errorCode, String errorDesc, Throwable t) {
777 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
778 logger.error(msg.toString(), normalize(arg0), t);
779 MDC.remove(TARGETENTITY);
780 MDC.remove(TARGETSERVICENAME);
784 * Record the Error event with 2 arguments
787 * The log message to put
789 * The arguments used in the log message
791 public void error(MessageEnum msg, String arg0, String arg1, String targetEntity,
792 String targetServiceName, ErrorCode errorCode, String errorDesc) {
793 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
794 logger.error(msg.toString(), normalize(arg0), normalize(arg1));
795 MDC.remove(TARGETENTITY);
796 MDC.remove(TARGETSERVICENAME);
800 * Record the Error event with 2 arguments
803 * The log message to put
805 * The arguments used in the log message
809 public void error(MessageEnum msg, String arg0, String arg1, String targetEntity,
810 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
811 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
812 logger.error(msg.toString(), normalize(arg0), normalize(arg1), t);
813 logger.debug("Exception raised", t);
814 MDC.remove(TARGETENTITY);
815 MDC.remove(TARGETSERVICENAME);
819 * Record the Error event with 3 arguments
822 * The log message to put
823 * @param arg0,arg1,arg2
824 * The arguments used in the log message
826 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
827 String targetServiceName, ErrorCode errorCode, String errorDesc) {
828 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
829 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2));
830 MDC.remove(TARGETENTITY);
831 MDC.remove(TARGETSERVICENAME);
835 * Record the Error event with 3 arguments
838 * The log message to put
839 * @param arg0,arg1,arg2
840 * The arguments used in the log message
844 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
845 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
846 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
847 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), t);
848 MDC.remove(TARGETENTITY);
849 MDC.remove(TARGETSERVICENAME);
853 * Record the Error event with 4 arguments
856 * The log message to put
857 * @param arg0,arg1,arg2,arg3
858 * The arguments used in the log message
860 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3,
861 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
862 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
863 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
864 MDC.remove(TARGETENTITY);
865 MDC.remove(TARGETSERVICENAME);
869 * Record the Error event with 4 arguments
872 * The log message to put
873 * @param arg0,arg1,arg2,arg3
874 * The arguments used in the log message
878 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3,
879 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
880 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
881 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), t);
883 logger.debug("Exception raised", t);
884 MDC.remove(TARGETENTITY);
885 MDC.remove(TARGETSERVICENAME);
889 * Record the Error event with 5 arguments
892 * The log message to put
893 * @param arg0,arg1,arg2,arg3,arg4
894 * The arguments used in the log message
896 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
897 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
898 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
899 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
900 MDC.remove(TARGETENTITY);
901 MDC.remove(TARGETSERVICENAME);
905 * Record the Error event with 5 arguments
908 * The log message to put
909 * @param arg0,arg1,arg2,arg3,arg4
910 * The arguments used in the log message
914 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
915 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
916 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
917 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4), t);
918 logger.debug("Exception raised", t);
919 MDC.remove(TARGETENTITY);
920 MDC.remove(TARGETSERVICENAME);
923 public void error(String errorMessage, String errorSource, String targetEntity, String targetServiceName,
924 ErrorCode errorCode, String errorText) {
925 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorText);
926 logger.error(errorMessage);
927 MDC.remove(TARGETENTITY);
928 MDC.remove(TARGETSERVICENAME);
932 public void logStackTrace(Exception ex){
933 StringWriter errors = new StringWriter();
934 ex.printStackTrace(new PrintWriter(errors));
935 logger.error(errors.toString());
938 public boolean isDebugEnabled() {
939 return logger.isDebugEnabled();
942 private void prepareMsg(String loggingLevel) {
943 prepareMsg(loggingLevel, null, null);
946 private void prepareMsg(String loggingLevel, String serviceNamep, String timer) {
947 String reqId = MDC.get(REQUEST_ID);
948 String svcId = MDC.get(SERVICE_INSTANCE_ID);
950 // Based on the discussion with Adrian,
951 // if these 2 parameters is not available, using dummy value "trace-#"
952 if (reqId == null || reqId.isEmpty()) {
953 MDC.put(REQUEST_ID, DUMMY_VALUE);
957 MDC.put(TIMER, timer);
960 writeIfNotNullorEmpty(SERVICE_NAME,getFinalServiceName(serviceNamep));
961 writeIfNotNullorEmpty(ALERT_SEVERITY,getSeverityLevel(loggingLevel));
962 writeIfNotNullorEmpty(INSTANCE_UUID,instanceUUID);
963 writeIfNotNullorEmpty(SERVER_IP,serverIP);
964 writeIfNotNullorEmpty(FQDN,serverName);
968 private void writeIfNotNullorEmpty(String Key, String value) {
969 if (MDC.get(Key) == null|| MDC.get(Key).isEmpty()) {
974 private void prepareAuditMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc) {
975 long endTime = System.currentTimeMillis();
976 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
977 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
980 private void prepareAuditMetricMsg(long startTime, long endTime, StatusCode statusCode, int responseCode,
981 String responseDesc) {
982 Date startDate = new Date(startTime);
983 Date endDate = new Date(endTime);
984 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
986 MDC.put(BEGINTIME, String.valueOf(formatter.format(startDate)));
987 MDC.put(ENDTIME, String.valueOf(formatter.format(endDate)));
988 MDC.put(STATUSCODE, statusCode.name());
989 MDC.put(RESPONSECODE, String.valueOf(responseCode));
990 MDC.put(RESPONSEDESC, responseDesc);
993 private void prepareErrorMsg(String loggingLevel, String targetEntity, String targetServiceName,
994 ErrorCode errorCode, String errorDesc) {
995 MDC.put(ALERT_SEVERITY, getSeverityLevel(loggingLevel));
996 if(errorCode != null) {
997 MDC.put(ERRORCODE, Integer.toString(errorCode.getValue()));
999 MDC.put(ERRORDESC, errorDesc);
1000 MDC.put(TARGETENTITY, targetEntity);
1001 MDC.put(TARGETSERVICENAME, targetServiceName);
1002 MDC.put(SERVICE_NAME, getFinalServiceName(getServiceName()));
1005 private void prepareMetricMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc,
1006 String targetEntity, String targetServiceName, String targetVEntity) {
1007 long endTime = System.currentTimeMillis();
1008 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
1009 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
1011 // Populate Metric log specific parameter
1012 MDC.put(TARGETENTITY, targetEntity);
1013 MDC.put(TARGETSERVICENAME, targetServiceName);
1015 if (null != targetVEntity) {
1016 MDC.put(TARGETVIRTUALENTITY, targetVEntity);
1020 private String getSeverityLevel(String loggingLevel) {
1022 // According to the Nagios alerting: 0=OK; 1=WARNING; 2=UNKOWN;
1024 switch (loggingLevel) {
1041 private String getFinalServiceName(String serviceNamep) {
1042 // This step to set the serviceName should be put after the className is
1044 // since the default serviceName is obtained during the method to get
1047 // There's 3 ways to set the serviceName. The first method has the most
1048 // priority to set the value.
1049 // a) If the serviceName is set within the log method, this value will
1051 // b) If serviceName is not set within the log method, the value defined
1052 // in the MDC will be used
1053 // c) If nothing is set specifically, then MsoLogger will assign a
1054 // default(MSO.<method_name>) value to it
1055 String serName = MDC.get(MsoLogger.SERVICE_NAME);
1057 // Check if service name was already set as the method name by a
1058 // previous call to this method.
1059 String isMethodNameStr = MDC.get(MsoLogger.SERVICE_NAME_IS_METHOD_NAME);
1060 boolean isMethodName = isMethodNameStr != null && isMethodNameStr.equals(Boolean.TRUE.toString());
1061 if (serviceNamep != null) {
1062 return serviceNamep;
1063 } else if (serName != null && !isMethodName) {
1067 MDC.put(MsoLogger.SERVICE_NAME_IS_METHOD_NAME, Boolean.TRUE.toString());
1069 StackTraceElement[] classArr = new Exception().getStackTrace();
1070 if (classArr.length >= 6) {
1073 limit = classArr.length;
1075 for (int i = 1; i < limit; i++) {
1076 if (!classArr[i].getClassName().equals(this.getClass().getName())) {
1077 return classArr[i].getMethodName();
1080 return classArr[0].getMethodName();
1083 // Based on the discussion with Adrian, instanceUUID is used to identifiy
1084 // the mso instance,
1085 // it is generated during mso instance initialization period
1086 // The same mso instnace will use the same instanceUUID value, even after
1088 private static String getInstanceUUID() {
1089 return System.getProperty("mso-instance-id");
1093 * Set the requestId and serviceInstanceId
1098 * The serviceInstanceId
1100 public static void setLogContext(String reqId, String svcId) {
1101 if (null != reqId) {
1102 MDC.put(REQUEST_ID, reqId);
1105 if (null != svcId) {
1106 MDC.put(SERVICE_INSTANCE_ID, svcId);
1111 * Set the remoteIp and the basic HTTP Authentication user
1114 * The remote ip address
1116 * The basic http authencitation user
1118 public static void setLoggerParameters(String remoteIpp, String userp) {
1119 if (null != remoteIpp) {
1120 MDC.put(REMOTE_HOST, remoteIpp);
1122 if (null != userp) {
1123 MDC.put(USER, userp);
1128 * Set the serviceName
1130 * @param serviceNamep
1133 public static void setServiceName(String serviceNamep) {
1134 if (null != serviceNamep) {
1135 MDC.put(SERVICE_NAME, serviceNamep);
1136 MDC.remove(SERVICE_NAME_IS_METHOD_NAME);
1141 * Get the serviceName
1143 * @return The service name
1145 public static String getServiceName() {
1146 return MDC.get(SERVICE_NAME);
1150 * Reset the serviceName
1152 public static void resetServiceName() {
1153 MDC.remove(SERVICE_NAME);
1157 * Set the requestId and serviceInstanceId based on the mso request
1162 public static void setLogContext(MsoRequest msoRequest) {
1163 if (msoRequest != null) {
1164 MDC.put(REQUEST_ID, msoRequest.getRequestId());
1165 MDC.put(SERVICE_INSTANCE_ID, msoRequest.getServiceInstanceId());
1167 MDC.put(REQUEST_ID, DUMMY_VALUE);
1168 MDC.put(SERVICE_INSTANCE_ID, DUMMY_VALUE);
1172 private String normalize(String input) {
1173 if (input == null) {
1176 String result = input.replace('|', '!');
1177 result = result.replace("\n", " - ");
1181 private void setDefaultLogCatalog(MsoLogger.Catalog cat) {
1182 if ("APIH".equals(cat.toString())) {
1183 exceptionArg = MessageEnum.APIH_GENERAL_EXCEPTION_ARG;
1184 defaultException = MessageEnum.APIH_GENERAL_EXCEPTION;
1185 defaultWarning = MessageEnum.APIH_GENERAL_WARNING;
1186 defaultAudit = MessageEnum.APIH_AUDIT_EXEC;
1187 defaultMetrics = MessageEnum.APIH_GENERAL_METRICS;
1188 } else if ("RA".equals(cat.toString())) {
1189 exceptionArg = MessageEnum.RA_GENERAL_EXCEPTION_ARG;
1190 defaultException = MessageEnum.RA_GENERAL_EXCEPTION;
1191 defaultWarning = MessageEnum.RA_GENERAL_WARNING;
1192 defaultAudit = MessageEnum.RA_AUDIT_EXEC;
1193 defaultMetrics = MessageEnum.RA_GENERAL_METRICS;
1194 } else if ("BPEL".equals(cat.toString())) {
1195 exceptionArg = MessageEnum.BPMN_GENERAL_EXCEPTION_ARG;
1196 defaultException = MessageEnum.BPMN_GENERAL_EXCEPTION;
1197 defaultWarning = MessageEnum.BPMN_GENERAL_WARNING;
1198 defaultAudit = MessageEnum.BPMN_AUDIT_EXEC;
1199 defaultMetrics = MessageEnum.BPMN_GENERAL_METRICS;
1200 } else if ("ASDC".equals(cat.toString())) {
1201 exceptionArg = MessageEnum.ASDC_GENERAL_EXCEPTION_ARG;
1202 defaultException = MessageEnum.ASDC_GENERAL_EXCEPTION;
1203 defaultWarning = MessageEnum.ASDC_GENERAL_WARNING;
1204 defaultAudit = MessageEnum.ASDC_AUDIT_EXEC;
1205 defaultMetrics = MessageEnum.ASDC_GENERAL_METRICS;
1207 exceptionArg = MessageEnum.GENERAL_EXCEPTION_ARG;
1208 defaultException = MessageEnum.GENERAL_EXCEPTION;
1209 defaultWarning = MessageEnum.GENERAL_WARNING;
1210 defaultAudit = MessageEnum.AUDIT_EXEC;
1211 defaultMetrics = MessageEnum.GENERAL_METRICS;
1215 public void warnSimple(String message, Exception e) {
1216 logger.warn(message,e);