aboutsummaryrefslogtreecommitdiffstats
path: root/osdf/logging/onap_common_v1/CommonLogger_testing.py
blob: 43e0ec3e006b786bf5aa3c39d72c435a9794bc7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python

# -------------------------------------------------------------------------
#   Copyright (c) 2015-2017 AT&T Intellectual Property
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# -------------------------------------------------------------------------
#
"""
Test the ONAP Common Logging library in Python.
CommonLogger_test.py
"""


from __future__ import print_function   # for the example code below parsing command line options
import os, sys, getopt                  # for the example code below parsing command line options

from osdf.logging.onap_common_v1.CommonLogger import CommonLogger                     # all that is needed to import the CommonLogger library

import uuid                             # to create UUIDs for our log records
import time                             # to create elapsed time for our log records


#----- A client might want to allow specifying the configFile as a command line option
usage="usage: %s [ -c <configFile> ]" % ( os.path.basename(__file__) )
try:
    opts, args = getopt.getopt(sys.argv[1:], "c:")
except getopt.GetoptError:
    print(usage, file=sys.stderr)
    sys.exit(2)

configFile = "CommonLogger_test.config"
for opt, arg in opts:
    if opt == "-c":
        configFile = arg
    else:
        print(usage, file=sys.stderr)
        sys.exit(2)


#----- Instantiate the loggers

# The client's top-level program (e.g., vPRO.py) can create a unique identifier UUID to differentiate between multiple instances of itself.
instanceUUID = uuid.uuid1()

# The client should identify its ONAP component -- and if applicable -- its ONAP sub-component
serviceName = "DCAE/vPRO"

# Instantiate using a configuration file with a key specifying the log file name and set fields' default values
errorLog =   CommonLogger.CommonLogger(configFile, "error",   instanceUUID=instanceUUID, serviceName=serviceName)
metricsLog = CommonLogger.CommonLogger(configFile, "metrics", instanceUUID=instanceUUID, serviceName=serviceName)
auditLog =   CommonLogger.CommonLogger(configFile, "audit",   instanceUUID=instanceUUID, serviceName=serviceName)
debugLog =   CommonLogger.CommonLogger(configFile, "debug",   instanceUUID=instanceUUID, serviceName=serviceName)


#----- use the loggers

# both metrics and audit logs can have an event starting time. This only affects the next log message.
metricsLog.setStartRecordEvent()
auditLog.setStartRecordEvent()

# Simple log messages
debugLog.debug("a DEBUG message for the debug log")
metricsLog.info("an INFO message for the metrics log")
auditLog.info("an INFO message for the audit log")
errorLog.warn("a WARN message for the error log")
errorLog.error("an ERROR message for the error log")
errorLog.fatal("a FATAL message for the error log")


# Can override any of the other fields when writing each log record
debugLog.debug("demonstrating overriding all fields with atypical values", requestID="2", serviceInstanceID="3", threadID="4", serverName="5", serviceName="6", instanceUUID="7", severity="9", serverIPAddress="10", server="11", IPAddress="12", className="13", timer="14")


# The is an example of an interaction between two ONAP components:

# vPRO generates Closed Loop RESTful API requests to App-C, knowing this information:
requestClient = "netman@localdcae.att.com:~/vPRO_trinity/vPRO.py:905"  # uniquely identifies the requester
requestTime = "2015-08-20 20:57:14.463426"                             # unique ID of the request within the requester's scope
request = "Restart"

# Form the value for Common Logging's requestID field:
requestID = requestClient + "+" + requestTime  # vPRO will use this as the unique requestID
# requestID = uuid.uuid1()  # other services might generate a UUID as their requestID

# Form the value for Common Logging's serviceName field when an interaction between two ONAP components:
ourONAP = serviceName
peerONAP = "App-C"
operation = request
interaction = ourONAP + ":" + peerONAP + "." + operation

# Let's calculate and report elapsed times
start = time.time()

# Log the request
auditLog.info("Requesting %s to %s" %(peerONAP, operation), requestID=requestID, serviceName=interaction)

# Wait for first response
time.sleep(1)  # simulate processing the action, e.g., waiting for response from App-C

# Form the value for Common Logging's serviceName field when an interaction between two ONAP components:
operation = 'PENDING'
interaction = peerONAP + ":" + ourONAP + "." + operation

# Log the response with elapsed time
ms = int(round(1000 * (time.time() - start)))  # Calculate elapsed time in ms
auditLog.info("%s acknowledged receiving request for %s" %(peerONAP, operation), requestID=requestID, serviceName=interaction, timer=ms)

# Wait for next response
time.sleep(1)  # simulate processing the action, e.g., waiting for response from App-C

# Form the value for Common Logging's serviceName field when an interaction between two ONAP components:
operation = 'SUCCESS'
interaction = peerONAP + ":" + ourONAP + "." + operation

# Log the response with elapsed time
ms = int(round(1000 * (time.time() - start)))  # Calculate elapsed time in ms
auditLog.info("%s finished %s" %(peerONAP, operation), requestID=requestID, serviceName=interaction, timer=ms)


# Can change the fields' default values for a logger after instantiation if desired
debugLog.setFields(serviceName="DCAE", threadID='thread-2')

# Then subsequent logging will have the new default field values
debugLog.info("Something happened")
debugLog.warn("Something happened again")


# Unset (set=None) a field so the Common Logger will use the default value
debugLog.info("threadID should be default", threadID=None)
debugLog.setFields(threadID=None)
debugLog.info("threadID should be default")