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.onap.so.entity.MsoRequest;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import org.slf4j.Marker;
39 import org.slf4j.MarkerFactory;
43 * This class supports all of the normal logging functions (debug, info, etc.), prepending
44 * a string of format "[<requestId>|<serviceId]" to each message.
46 * SO code should initialize with these IDs when available, so that individual
47 * requests and/or services can be tracked throughout the various MSO component
48 * logs (API Handler, BPEL, and Adapters).
53 public class MsoLogger {
54 // Required MDC parameters
55 public static final String REQUEST_ID = "RequestID";
56 public static final String INVOCATION_ID = "InvocationID";
57 public static final String INSTANCE_UUID = "InstanceUUID";
58 public static final String SERVICE_NAME = "ServiceName";
59 public static final String STATUSCODE = "StatusCode";
60 public static final String RESPONSECODE = "ResponseCode";
61 public static final String RESPONSEDESC = "ResponseDesc";
62 public static final String FQDN = "ServerFQDN";
63 public static final String ENTRY_TIMESTAMP = "EntryTimestamp";
64 public static final String CLIENT_IPADDRESS = "EntryTimestamp";
68 public static final String HEADER_FROM_APP_ID = "X-FromAppId";
69 public static final String ONAP_PARTNER_NAME = "X-ONAP-PartnerName";
70 public static final String HEADER_REQUEST_ID = "X-RequestId";
71 public static final String TRANSACTION_ID = "X-TransactionID";
72 public static final String ECOMP_REQUEST_ID = "X-ECOMP-RequestID";
73 public static final String ONAP_REQUEST_ID = "X-ONAP-RequestID";
74 public static final String CLIENT_ID = "X-ClientID";
75 public static final String INVOCATION_ID_HEADER = "X-InvocationID";
77 //Default values for not found
78 public static final String UNKNOWN_PARTNER = "UnknownPartner";
80 public static final String SERVICE_INSTANCE_ID = "ServiceInstanceId";
81 public static final String SERVICE_NAME_IS_METHOD_NAME = "ServiceNameIsMethodName";
82 public static final String SERVER_IP = "ServerIPAddress";
84 public static final String REMOTE_HOST = "RemoteHost";
85 public static final String ALERT_SEVERITY = "AlertSeverity";
86 public static final String TIMER = "Timer";
87 public static final String USER = "User";
88 public static final String DUMMY_VALUE = "trace-#";
89 public static final String UNKNOWN = "UNKNOWN";
90 public static final String CAT_LOG_LEVEL = "CategoryLogLevel";
91 public static final String AUDI_CAT_LOG_LEVEL = "AuditCategoryLogLevel";
95 public static final String PARTNER_NAME = "PartnerName";
103 // Audit/Metric log specific
104 public static final String BEGINTIME = "BeginTimestamp";
105 public static final String STARTTIME = "StartTimeMilis";
106 public static final String ENDTIME = "EndTimestamp";
107 public static final String PARTNERNAME = "PartnerName";
111 // Metric log specific
112 public static final String METRIC_BEGIN_TIME = "MetricBeginTime";
113 public static final String METRIC_START_TIME = "MetricStartTime";
114 public static final String METRIC_END_TIME = "MetricEndTime";
115 public static final String METRIC_TIMER = "MetricEndTime";
116 public static final String TARGETENTITY = "TargetEntity";
117 public static final String TARGETSERVICENAME = "TargetServiceName";
118 public static final String TARGETVIRTUALENTITY = "TargetVirtualEntity";
120 public static final String FATAL_LEVEL = "FATAL";
121 public static final String ERROR_LEVEL = "ERROR";
122 public static final String WARN_LEVEL = "WARN";
123 public static final String INFO_LEVEL = "INFO";
124 public static final String DEBUG_LEVEL = "DEBUG";
126 public static final String ERRORCODE = "ErrorCode";
127 public static final String ERRORDESC = "ErrorDesc";
130 public static final String COMPLETE = "COMPLETE";
131 public static final String INPROGRESS = "INPROGRESS";
133 public enum Catalog {
134 APIH, BPEL, RA, ASDC, GENERAL
137 public enum StatusCode {
141 public enum TargetEntity {
146 TargetEntity(String name) {
150 public String getUrl() {
155 public enum ResponseCode {
156 Suc(0), PermissionError(100), DataError(300), DataNotFound(301), BadRequest(302), SchemaError(
157 400), BusinessProcesssError(500), ServiceNotAvailable(501), InternalError(
158 502), Conflict(503), DBAccessError(504), CommunicationError(505), UnknownError(900);
162 public int getValue() {
166 private ResponseCode(int value) {
171 public enum ErrorCode {
172 PermissionError(100), AvailabilityError(200), DataError(300), SchemaError(400), BusinessProcesssError(
173 500), UnknownError(900);
177 public int getValue() {
181 private ErrorCode(int value) {
186 public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
187 public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
189 private Logger logger;
190 private Logger metricsLogger;
191 private Logger auditLogger;
192 private static String instanceUUID, serverIP, serverName;
193 private MessageEnum exceptionArg, defaultException, defaultWarning, defaultAudit, defaultMetrics;
195 // For internal logging of the initialization of MSO logs
196 private static final Logger initLOGGER = LoggerFactory.getLogger(MsoLogger.class.getName());
198 private MsoLogger() {
199 this(MsoLogger.Catalog.GENERAL);
202 private MsoLogger(MsoLogger.Catalog cat) {
203 this(cat, MethodHandles.lookup().lookupClass());
206 private MsoLogger(MsoLogger.Catalog cat, Class<?> clazz) {
207 this.logger = LoggerFactory.getLogger(clazz);
208 this.auditLogger = LoggerFactory.getLogger("AUDIT");
209 this.metricsLogger = LoggerFactory.getLogger("METRIC");
210 MsoLogger.initialization();
211 setDefaultLogCatalog(cat);
214 private static synchronized void initialization() {
215 if (instanceUUID == null || ("").equals(instanceUUID)) {
216 instanceUUID = getInstanceUUID();
219 if (serverIP == null || serverName == null || ("").equals(serverIP) || ("").equals(serverName)) {
221 InetAddress server = InetAddress.getLocalHost();
222 serverIP = server.getHostAddress();
223 serverName = server.getHostName();
224 } catch (UnknownHostException e) {
225 initLOGGER.error("Could not get local hostname", e);
234 public static MsoLogger getMsoLogger(MsoLogger.Catalog cat, Class<?> clazz) {
235 return new MsoLogger(cat,clazz);
241 * Record the Metrics event with no argument
244 * Transaction starting time in millieseconds
246 * StatusCode of the transaction, either COMPLETE or ERROR
247 * @param responseCode
248 * The response code returned by the sub-components
249 * @param responseDesc
250 * Human redable description of the response code
251 * @param targetEntity
252 * The component which is invoked for this sub-operation
253 * @param targetServiceName
254 * API invoked on the TargetEntity
255 * @param targetVEntity
256 * Target VNF or VM acted opon by the component, if available
258 public void recordMetricEvent() {
259 metricsLogger.info("");
261 MDC.remove(TARGETENTITY);
262 MDC.remove(TARGETSERVICENAME);
265 public void recordMetricEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode, String responseDesc,
266 String targetEntity, String targetServiceName, String targetVEntity) {
267 prepareMetricMsg(startTime, statusCode, responseCode.getValue(), responseDesc, targetEntity, targetServiceName,
269 metricsLogger.info("");
271 MDC.remove(TARGETENTITY);
272 MDC.remove(TARGETSERVICENAME);
277 * Record the Audit event
280 * Transaction starting time in millieseconds
282 * StatusCode of the transaction, either COMPLETE or ERROR
283 * @param responseCode
284 * The application specific response code
285 * @param responseDesc
286 * Human redable description of the application response code
288 public void recordAuditEvent(){
289 auditLogger.info("");
292 public void recordAuditEvent(Long startTime, StatusCode statusCode, ResponseCode responseCode,
293 String responseDesc) {
294 MDC.put(MsoLogger.PARTNERNAME, "UNKNOWN");
295 prepareAuditMsg(startTime, statusCode, responseCode.getValue(), responseDesc);
296 auditLogger.info("");
302 * Record the Debug event
305 * The log message to put
307 public void debug(String msg) {
308 prepareMsg(DEBUG_LEVEL);
313 * Record the Debug event
316 * The log message to put
318 * The exception to put
320 public void debug(String msg, Throwable t) {
321 prepareMsg(DEBUG_LEVEL);
322 logger.debug(msg, t);
325 public void info(String msg) {
326 prepareMsg(DEBUG_LEVEL);
332 * Log error message with the details of the exception that caused the error.
336 public void error(String msg) {
337 prepareMsg(ERROR_LEVEL);
342 * Log error message with the details of the exception that caused the error.
346 public void error(String msg, Throwable throwable) {
347 prepareMsg(ERROR_LEVEL);
348 logger.error(msg, throwable);
353 * Record the Info event
356 * The log message to put
358 public void info(String msg, String targetEntity, String targetServiceName) {
359 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
362 MDC.remove(TARGETENTITY);
363 MDC.remove(TARGETSERVICENAME);
367 * Record the Info event with 1 argument
370 * The log message to put
372 * The argument used in the log message
374 public void info(MessageEnum msg, String arg0, String targetEntity, String targetServiceName) {
375 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
377 logger.info(msg.toString(), normalize(arg0));
378 MDC.remove(TARGETENTITY);
379 MDC.remove(TARGETSERVICENAME);
383 * Record the Info event with 2 arguments
386 * The log message to put
388 * The arguments used in the log message
390 public void info(String msg, String arg0, String arg1, String targetEntity,
391 String targetServiceName) {
392 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
394 logger.info(msg, normalize(arg0), normalize(arg1));
395 MDC.remove(TARGETENTITY);
396 MDC.remove(TARGETSERVICENAME);
400 * Record the Info event with 3 arguments
403 * The log message to put
404 * @param arg0,arg1,arg2
405 * The arguments used in the log message
407 public void info(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
408 String targetServiceName) {
409 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
411 logger.info(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2));
412 MDC.remove(TARGETENTITY);
413 MDC.remove(TARGETSERVICENAME);
417 * Record the Info event with 4 arguments
420 * The log message to put
421 * @param arg0,arg1,arg2,arg3
422 * The arguments used in the log message
424 public void info(String msg, String arg0, String arg1, String arg2, String arg3,
425 String targetEntity, String targetServiceName) {
426 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
428 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
429 MDC.remove(TARGETENTITY);
430 MDC.remove(TARGETSERVICENAME);
434 * Record the Info event with 5 arguments
437 * The log message to put
438 * @param arg0,arg1,arg2,arg3,arg4
439 * The arguments used in the log message
441 public void info(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
442 String targetEntity, String targetServiceName) {
443 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
445 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
446 MDC.remove(TARGETENTITY);
447 MDC.remove(TARGETSERVICENAME);
451 * Record the Info event with 6 arguments
454 * The log message to put
455 * @param arg0,arg1,arg2,arg3,arg4,arg5
456 * The arguments used in the log message
458 public void info(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
459 String arg5, String targetEntity, String targetServiceName) {
460 prepareErrorMsg(INFO_LEVEL, targetEntity, targetServiceName, null, "");
462 logger.info(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4),
464 MDC.remove(TARGETENTITY);
465 MDC.remove(TARGETSERVICENAME);
472 * Record the Warning event
475 * The log message to put
477 public void warnSimple( String targetServiceName, String errorDesc) {
478 logger.warn("Service Name: {} Error: {}" , targetServiceName, errorDesc);
481 * Record the Warning event
484 * The log message to put
486 public void warn(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
488 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
490 logger.warn(msg.toString());
491 MDC.remove(TARGETENTITY);
492 MDC.remove(TARGETSERVICENAME);
496 * Record the Warning event
499 * The log message to put
503 public void warn(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
504 String errorDesc, Throwable t) {
505 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
506 logger.warn("Warning: "+msg, t);
507 MDC.remove(TARGETENTITY);
508 MDC.remove(TARGETSERVICENAME);
512 * Record the Warn event with 1 argument
515 * The log message to put
517 * The argument used in the log message
519 public void warn(MessageEnum msg, String arg, String targetEntity, String targetServiceName,
520 ErrorCode errorCode, String errorDesc) {
521 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
522 logger.warn(msg.toString(), arg);
523 MDC.remove(TARGETENTITY);
524 MDC.remove(TARGETSERVICENAME);
528 * Record the Warn event with 1 argument
531 * The log message to put
533 * The arguments used in the log message
537 public void warn(MessageEnum msg, String arg, String targetEntity, String targetServiceName,
538 ErrorCode errorCode, String errorDesc, Throwable t) {
539 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
540 logger.warn(msg.toString(), arg);
541 logger.debug("Exception raised", t);
542 MDC.remove(TARGETENTITY);
543 MDC.remove(TARGETSERVICENAME);
547 * Record the Warn event with 2 arguments
550 * The log message to put
552 * The arguments used in the log message
554 public void warn(MessageEnum msg, String arg0, String arg1, String targetEntity,
555 String targetServiceName, ErrorCode errorCode, String errorDesc) {
556 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
557 logger.warn(msg.toString(), normalize(arg0), normalize(arg1));
558 MDC.remove(TARGETENTITY);
559 MDC.remove(TARGETSERVICENAME);
563 * Record the Warn event with 2 arguments
566 * The log message to put
568 * The arguments used in the log message
572 public void warn(String msg, String arg0, String arg1, String targetEntity,
573 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
574 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
575 logger.warn(msg, normalize(arg0), normalize(arg1));
577 MDC.remove(TARGETENTITY);
578 MDC.remove(TARGETSERVICENAME);
582 * Record the Warn event with 3 arguments
585 * The log message to put
586 * @param arg0,arg1,arg2
587 * The arguments used in the log message
589 public void warn(String msg, String arg0, String arg1, String arg2, String targetEntity,
590 String targetServiceName, ErrorCode errorCode, String errorDesc) {
591 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
592 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
593 MDC.remove(TARGETENTITY);
594 MDC.remove(TARGETSERVICENAME);
598 * Record the Warn event with 3 arguments
601 * The log message to put
602 * @param arg0,arg1,arg2
603 * The arguments used in the log message
607 public void warn(String msg, String arg0, String arg1, String arg2, String targetEntity,
608 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
609 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
610 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2));
612 MDC.remove(TARGETENTITY);
613 MDC.remove(TARGETSERVICENAME);
617 * Record the Warn event with 4 arguments
620 * The log message to put
621 * @param arg0,arg1,arg2,arg3
622 * The arguments used in the log message
624 public void warn(String msg, String arg0, String arg1, String arg2, String arg3,
625 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
626 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
627 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
628 MDC.remove(TARGETENTITY);
629 MDC.remove(TARGETSERVICENAME);
633 * Record the Warn event with 4 arguments
636 * The log message to put
637 * @param arg0,arg1,arg2,
638 * arg3 The arguments used in the log message
642 public void warn(String msg, String arg0, String arg1, String arg2, String arg3,
643 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
644 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
645 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
647 MDC.remove(TARGETENTITY);
648 MDC.remove(TARGETSERVICENAME);
652 * Record the Warn event with 5 arguments
655 * The log message to put
656 * @param arg0,arg1,arg2,arg3,arg4
657 * The arguments used in the log message
659 public void warn(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
660 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
661 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
662 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
663 MDC.remove(TARGETENTITY);
664 MDC.remove(TARGETSERVICENAME);
668 * Record the Warn event with 5 arguments
671 * The log message to put
672 * @param arg0,arg1,arg2,arg3,arg4
673 * The arguments used in the log message
677 public void warn(String msg, String arg0, String arg1, String arg2, String arg3, String arg4,
678 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
679 prepareErrorMsg(WARN_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
680 logger.warn(msg, normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
682 MDC.remove(TARGETENTITY);
683 MDC.remove(TARGETSERVICENAME);
690 * Record the Error event
692 * @param generalException
693 * The log message to put
695 public void error(MessageEnum generalException, String targetEntity, String targetServiceName, ErrorCode errorCode,
697 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
698 logger.error(generalException.toString() + ": " +errorDesc);
699 MDC.remove(TARGETENTITY);
700 MDC.remove(TARGETSERVICENAME);
705 * Record the Error event
708 * The log message to put
712 public void trace(String traceMessage) {
713 logger.trace(traceMessage);
718 * Record the Error event
721 * The log message to put
725 public void error( Throwable t) {
726 logger.error(t.getMessage(), t);
731 * Record the Error event
734 * The log message to put
738 public void error(MessageEnum msg, String targetEntity, String targetServiceName, ErrorCode errorCode,
739 String errorDesc, Throwable t) {
740 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
741 logger.error(msg.toString(), t);
742 MDC.remove(TARGETENTITY);
743 MDC.remove(TARGETSERVICENAME);
747 * Record the Error event with 1 argument
750 * The log message to put
752 * The arguments used in the log message
754 public void error(MessageEnum msg, String arg0, String targetEntity, String targetServiceName,
755 ErrorCode errorCode, String errorDesc) {
756 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
757 logger.error(msg.toString(), normalize(arg0));
758 MDC.remove(TARGETENTITY);
759 MDC.remove(TARGETSERVICENAME);
763 * Record the Error event with 1 argument
766 * The log message to put
768 * The arguments used in the log message
772 public void error(MessageEnum msg, String arg0, String targetEntity, String targetServiceName,
773 ErrorCode errorCode, String errorDesc, Throwable t) {
774 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
775 logger.error(msg.toString(), normalize(arg0), t);
776 MDC.remove(TARGETENTITY);
777 MDC.remove(TARGETSERVICENAME);
781 * Record the Error event with 2 arguments
784 * The log message to put
786 * The arguments used in the log message
788 public void error(MessageEnum msg, String arg0, String arg1, String targetEntity,
789 String targetServiceName, ErrorCode errorCode, String errorDesc) {
790 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
791 logger.error(msg.toString(), normalize(arg0), normalize(arg1));
792 MDC.remove(TARGETENTITY);
793 MDC.remove(TARGETSERVICENAME);
797 * Record the Error event with 2 arguments
800 * The log message to put
802 * The arguments used in the log message
806 public void error(MessageEnum msg, String arg0, String arg1, String targetEntity,
807 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
808 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
809 logger.error(msg.toString(), normalize(arg0), normalize(arg1), t);
810 logger.debug("Exception raised", t);
811 MDC.remove(TARGETENTITY);
812 MDC.remove(TARGETSERVICENAME);
816 * Record the Error event with 3 arguments
819 * The log message to put
820 * @param arg0,arg1,arg2
821 * The arguments used in the log message
823 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
824 String targetServiceName, ErrorCode errorCode, String errorDesc) {
825 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
826 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2));
827 MDC.remove(TARGETENTITY);
828 MDC.remove(TARGETSERVICENAME);
832 * Record the Error event with 3 arguments
835 * The log message to put
836 * @param arg0,arg1,arg2
837 * The arguments used in the log message
841 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String targetEntity,
842 String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
843 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
844 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), t);
845 MDC.remove(TARGETENTITY);
846 MDC.remove(TARGETSERVICENAME);
850 * Record the Error event with 4 arguments
853 * The log message to put
854 * @param arg0,arg1,arg2,arg3
855 * The arguments used in the log message
857 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3,
858 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
859 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
860 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3));
861 MDC.remove(TARGETENTITY);
862 MDC.remove(TARGETSERVICENAME);
866 * Record the Error event with 4 arguments
869 * The log message to put
870 * @param arg0,arg1,arg2,arg3
871 * The arguments used in the log message
875 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3,
876 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
877 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
878 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), t);
880 logger.debug("Exception raised", t);
881 MDC.remove(TARGETENTITY);
882 MDC.remove(TARGETSERVICENAME);
886 * Record the Error event with 5 arguments
889 * The log message to put
890 * @param arg0,arg1,arg2,arg3,arg4
891 * The arguments used in the log message
893 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
894 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc) {
895 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
896 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4));
897 MDC.remove(TARGETENTITY);
898 MDC.remove(TARGETSERVICENAME);
902 * Record the Error event with 5 arguments
905 * The log message to put
906 * @param arg0,arg1,arg2,arg3,arg4
907 * The arguments used in the log message
911 public void error(MessageEnum msg, String arg0, String arg1, String arg2, String arg3, String arg4,
912 String targetEntity, String targetServiceName, ErrorCode errorCode, String errorDesc, Throwable t) {
913 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorDesc);
914 logger.error(msg.toString(), normalize(arg0), normalize(arg1), normalize(arg2), normalize(arg3), normalize(arg4), t);
915 logger.debug("Exception raised", t);
916 MDC.remove(TARGETENTITY);
917 MDC.remove(TARGETSERVICENAME);
920 public void error(String errorMessage, String errorSource, String targetEntity, String targetServiceName,
921 ErrorCode errorCode, String errorText) {
922 prepareErrorMsg(ERROR_LEVEL, targetEntity, targetServiceName, errorCode, errorText);
923 logger.error(errorMessage);
924 MDC.remove(TARGETENTITY);
925 MDC.remove(TARGETSERVICENAME);
929 public void logStackTrace(Exception ex){
930 StringWriter errors = new StringWriter();
931 ex.printStackTrace(new PrintWriter(errors));
932 logger.error(errors.toString());
935 public boolean isDebugEnabled() {
936 return logger.isDebugEnabled();
939 private void prepareMsg(String loggingLevel) {
940 prepareMsg(loggingLevel, null, null);
943 private void prepareMsg(String loggingLevel, String serviceNamep, String timer) {
944 String reqId = MDC.get(REQUEST_ID);
945 String svcId = MDC.get(SERVICE_INSTANCE_ID);
947 // Based on the discussion with Adrian,
948 // if these 2 parameters is not available, using dummy value "trace-#"
949 if (reqId == null || reqId.isEmpty()) {
950 MDC.put(REQUEST_ID, DUMMY_VALUE);
954 MDC.put(TIMER, timer);
957 writeIfNotNullorEmpty(SERVICE_NAME,getFinalServiceName(serviceNamep));
958 writeIfNotNullorEmpty(ALERT_SEVERITY,getSeverityLevel(loggingLevel));
959 writeIfNotNullorEmpty(INSTANCE_UUID,instanceUUID);
960 writeIfNotNullorEmpty(SERVER_IP,serverIP);
961 writeIfNotNullorEmpty(FQDN,serverName);
965 private void writeIfNotNullorEmpty(String Key, String value) {
966 if (MDC.get(Key) == null|| MDC.get(Key).isEmpty()) {
971 private void prepareAuditMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc) {
972 long endTime = System.currentTimeMillis();
973 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
974 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
977 private void prepareAuditMetricMsg(long startTime, long endTime, StatusCode statusCode, int responseCode,
978 String responseDesc) {
979 Date startDate = new Date(startTime);
980 Date endDate = new Date(endTime);
981 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
983 MDC.put(BEGINTIME, String.valueOf(formatter.format(startDate)));
984 MDC.put(ENDTIME, String.valueOf(formatter.format(endDate)));
985 MDC.put(STATUSCODE, statusCode.name());
986 MDC.put(RESPONSECODE, String.valueOf(responseCode));
987 MDC.put(RESPONSEDESC, responseDesc);
990 private void prepareErrorMsg(String loggingLevel, String targetEntity, String targetServiceName,
991 ErrorCode errorCode, String errorDesc) {
992 MDC.put(ALERT_SEVERITY, getSeverityLevel(loggingLevel));
993 if(errorCode != null) {
994 MDC.put(ERRORCODE, Integer.toString(errorCode.getValue()));
996 MDC.put(ERRORDESC, errorDesc);
997 MDC.put(TARGETENTITY, targetEntity);
998 MDC.put(TARGETSERVICENAME, targetServiceName);
999 MDC.put(SERVICE_NAME, getFinalServiceName(getServiceName()));
1002 private void prepareMetricMsg(long startTime, StatusCode statusCode, int responseCode, String responseDesc,
1003 String targetEntity, String targetServiceName, String targetVEntity) {
1004 long endTime = System.currentTimeMillis();
1005 prepareMsg(INFO_LEVEL, null, String.valueOf(endTime - startTime));
1006 prepareAuditMetricMsg(startTime, endTime, statusCode, responseCode, responseDesc);
1008 // Populate Metric log specific parameter
1009 MDC.put(TARGETENTITY, targetEntity);
1010 MDC.put(TARGETSERVICENAME, targetServiceName);
1012 if (null != targetVEntity) {
1013 MDC.put(TARGETVIRTUALENTITY, targetVEntity);
1017 private String getSeverityLevel(String loggingLevel) {
1019 // According to the Nagios alerting: 0=OK; 1=WARNING; 2=UNKOWN;
1021 switch (loggingLevel) {
1038 private String getFinalServiceName(String serviceNamep) {
1039 // This step to set the serviceName should be put after the className is
1041 // since the default serviceName is obtained during the method to get
1044 // There's 3 ways to set the serviceName. The first method has the most
1045 // priority to set the value.
1046 // a) If the serviceName is set within the log method, this value will
1048 // b) If serviceName is not set within the log method, the value defined
1049 // in the MDC will be used
1050 // c) If nothing is set specifically, then MsoLogger will assign a
1051 // default(MSO.<method_name>) value to it
1052 String serName = MDC.get(MsoLogger.SERVICE_NAME);
1054 // Check if service name was already set as the method name by a
1055 // previous call to this method.
1056 String isMethodNameStr = MDC.get(MsoLogger.SERVICE_NAME_IS_METHOD_NAME);
1057 boolean isMethodName = isMethodNameStr != null && isMethodNameStr.equals(Boolean.TRUE.toString());
1058 if (serviceNamep != null) {
1059 return serviceNamep;
1060 } else if (serName != null && !isMethodName) {
1064 MDC.put(MsoLogger.SERVICE_NAME_IS_METHOD_NAME, Boolean.TRUE.toString());
1066 StackTraceElement[] classArr = new Exception().getStackTrace();
1067 if (classArr.length >= 6) {
1070 limit = classArr.length;
1072 for (int i = 1; i < limit; i++) {
1073 if (!classArr[i].getClassName().equals(this.getClass().getName())) {
1074 return classArr[i].getMethodName();
1077 return classArr[0].getMethodName();
1080 // Based on the discussion with Adrian, instanceUUID is used to identifiy
1081 // the mso instance,
1082 // it is generated during mso instance initialization period
1083 // The same mso instnace will use the same instanceUUID value, even after
1085 private static String getInstanceUUID() {
1086 return System.getProperty("mso-instance-id");
1090 * Set the requestId and serviceInstanceId
1095 * The serviceInstanceId
1097 public static void setLogContext(String reqId, String svcId) {
1098 if (null != reqId) {
1099 MDC.put(REQUEST_ID, reqId);
1102 if (null != svcId) {
1103 MDC.put(SERVICE_INSTANCE_ID, svcId);
1108 * Set the remoteIp and the basic HTTP Authentication user
1111 * The remote ip address
1113 * The basic http authencitation user
1115 public static void setLoggerParameters(String remoteIpp, String userp) {
1116 if (null != remoteIpp) {
1117 MDC.put(REMOTE_HOST, remoteIpp);
1119 if (null != userp) {
1120 MDC.put(USER, userp);
1125 * Set the serviceName
1127 * @param serviceNamep
1130 public static void setServiceName(String serviceNamep) {
1131 if (null != serviceNamep) {
1132 MDC.put(SERVICE_NAME, serviceNamep);
1133 MDC.remove(SERVICE_NAME_IS_METHOD_NAME);
1138 * Get the serviceName
1140 * @return The service name
1142 public static String getServiceName() {
1143 return MDC.get(SERVICE_NAME);
1147 * Reset the serviceName
1149 public static void resetServiceName() {
1150 MDC.remove(SERVICE_NAME);
1154 * Set the requestId and serviceInstanceId based on the mso request
1159 public static void setLogContext(MsoRequest msoRequest) {
1160 if (msoRequest != null) {
1161 MDC.put(REQUEST_ID, msoRequest.getRequestId());
1162 MDC.put(SERVICE_INSTANCE_ID, msoRequest.getServiceInstanceId());
1164 MDC.put(REQUEST_ID, DUMMY_VALUE);
1165 MDC.put(SERVICE_INSTANCE_ID, DUMMY_VALUE);
1169 private String normalize(String input) {
1170 if (input == null) {
1173 String result = input.replace('|', '!');
1174 result = result.replace("\n", " - ");
1178 private void setDefaultLogCatalog(MsoLogger.Catalog cat) {
1179 if ("APIH".equals(cat.toString())) {
1180 exceptionArg = MessageEnum.APIH_GENERAL_EXCEPTION_ARG;
1181 defaultException = MessageEnum.APIH_GENERAL_EXCEPTION;
1182 defaultWarning = MessageEnum.APIH_GENERAL_WARNING;
1183 defaultAudit = MessageEnum.APIH_AUDIT_EXEC;
1184 defaultMetrics = MessageEnum.APIH_GENERAL_METRICS;
1185 } else if ("RA".equals(cat.toString())) {
1186 exceptionArg = MessageEnum.RA_GENERAL_EXCEPTION_ARG;
1187 defaultException = MessageEnum.RA_GENERAL_EXCEPTION;
1188 defaultWarning = MessageEnum.RA_GENERAL_WARNING;
1189 defaultAudit = MessageEnum.RA_AUDIT_EXEC;
1190 defaultMetrics = MessageEnum.RA_GENERAL_METRICS;
1191 } else if ("BPEL".equals(cat.toString())) {
1192 exceptionArg = MessageEnum.BPMN_GENERAL_EXCEPTION_ARG;
1193 defaultException = MessageEnum.BPMN_GENERAL_EXCEPTION;
1194 defaultWarning = MessageEnum.BPMN_GENERAL_WARNING;
1195 defaultAudit = MessageEnum.BPMN_AUDIT_EXEC;
1196 defaultMetrics = MessageEnum.BPMN_GENERAL_METRICS;
1197 } else if ("ASDC".equals(cat.toString())) {
1198 exceptionArg = MessageEnum.ASDC_GENERAL_EXCEPTION_ARG;
1199 defaultException = MessageEnum.ASDC_GENERAL_EXCEPTION;
1200 defaultWarning = MessageEnum.ASDC_GENERAL_WARNING;
1201 defaultAudit = MessageEnum.ASDC_AUDIT_EXEC;
1202 defaultMetrics = MessageEnum.ASDC_GENERAL_METRICS;
1204 exceptionArg = MessageEnum.GENERAL_EXCEPTION_ARG;
1205 defaultException = MessageEnum.GENERAL_EXCEPTION;
1206 defaultWarning = MessageEnum.GENERAL_WARNING;
1207 defaultAudit = MessageEnum.AUDIT_EXEC;
1208 defaultMetrics = MessageEnum.GENERAL_METRICS;
1212 public void warnSimple(String message, Exception e) {
1213 logger.warn(message,e);