1
- # coding: utf-8
1
+ # coding: utf-8
2
2
"""
3
3
Logs API client: It formats and submit REST API calls to LogicMonitor.
4
4
"""
@@ -52,10 +52,12 @@ def send_logs(self, **kwargs): # noqa: E501
52
52
53
53
Args:
54
54
resource (:class:`logicmonitor_data_sdk.models.resource.Resource`): The Resource object.
55
- logs (:obj:`dict`): The logs details in dictonary. e.g. {'msg' : 'This is sample logs'}.
55
+ msg (:obj:`str`): The log message. e.g. msg = "this is sample log msg".
56
+ timestamp (:obj:`str` or :obj:`int`, Optional): The timestamp when the event occurred. Supported date formats are ISO8601 and Unix Epoch (in secs, ms, ns).
57
+ metadata (:obj:`dict`,Optional): Metadata which can be used for defining logsource and other properties.
56
58
57
59
Return:
58
- If in :class:`Metrics ` batching is enabled then None
60
+ If in :class:`Logs ` batching is enabled then None
59
61
Otherwise the REST response will be return.
60
62
61
63
Examples:
@@ -66,10 +68,10 @@ def send_logs(self, **kwargs): # noqa: E501
66
68
>>>
67
69
>>> conf = Configuration(company="ACCOUNT_NAME", id= 'API_ACCESS_ID', key= 'API_ACCESS_KEY')
68
70
>>> # Create the Log client with batching enable
69
- >>> log_api = Logs()
70
- >>> # Create the Resource object using the 'system.deviceId ' properties.
71
+ >>> log_api = Logs() # By default batching is enabled with interval of 30 sec.
72
+ >>> # Create the Resource object using the 'system.hostname ' properties.
71
73
>>> resource = Resource(ids={"system.hostname": "SampleDevice"}, name="SampleDevice", properties={'using.sdk': 'true'})
72
- >>> log_api.send_logs(resource=resource, logs={' msg' : 'This is sample logs'} )
74
+ >>> log_api.send_logs(resource=resource, msg = "this is a sample log" )
73
75
"""
74
76
75
77
"""LogIngestApi # noqa: E501
@@ -88,28 +90,34 @@ def send_logs(self, **kwargs): # noqa: E501
88
90
returns the request thread.
89
91
"""
90
92
91
- all_params = ['resource' , 'logs ' ] # noqa: E501
93
+ all_params = ['resource' , 'msg' , 'timestamp' , 'metadata ' ] # noqa: E501
92
94
params = locals ()
93
95
for key , val in six .iteritems (params ['kwargs' ]):
94
96
if key not in all_params :
95
97
raise TypeError (
96
- "Got an unexpected keyword argument '%s' to method SendMetrics " % key
98
+ "Got an unexpected keyword argument '%s' to method send_logs() " % key
97
99
)
98
100
params [key ] = val
99
101
del params ['kwargs' ]
100
102
del params ['self' ]
101
103
del params ['all_params' ]
102
104
for one in all_params :
103
- if not params .__contains__ (one ):
105
+ if ( one != 'timestamp' and one != 'metadata' ) and ( not params .__contains__ (one ) ):
104
106
raise TypeError (
105
107
"Some arguments are missing keys='%s'" %
106
- str ( params . keys ())
108
+ one
107
109
)
108
110
# logger.debug("Request Send for {}".format(str(params['resource'].ids)))
109
111
if self .batch :
110
112
# self.add_request(**kwargs)
113
+ logs = {}
114
+ logs ['msg' ] = kwargs ['msg' ]
115
+ if kwargs .__contains__ ('timestamp' ):
116
+ logs ['timestamp' ] = kwargs ['timestamp' ]
117
+ if kwargs .__contains__ ('metadata' ):
118
+ logs ['metadata' ] = kwargs ['metadata' ]
111
119
self .add_request (resource = copy .deepcopy (kwargs ['resource' ]),
112
- logs = kwargs [ ' logs' ] )
120
+ logs = logs )
113
121
else :
114
122
return self ._single_request (** kwargs )
115
123
@@ -149,13 +157,18 @@ def _do_request(self):
149
157
def _merge_request (self , single_request ):
150
158
resource = single_request ['resource' ]
151
159
logs = single_request ['logs' ]
152
- logs ['_lm.resourceId' ] = resource .ids
160
+ logs ['_lm.resourceId' ] = resource .ids
153
161
self ._payload_cache .append (logs )
154
162
155
163
def _single_request (self , ** kwargs ):
156
164
resource = kwargs ['resource' ]
157
- logs = kwargs ['logs' ]
165
+ logs = {}
166
+ logs ['msg' ]= kwargs ['msg' ]
158
167
logs ['_lm.resourceId' ] = resource .ids
168
+ if kwargs .__contains__ ('timestamp' ):
169
+ logs ['timestamp' ] = kwargs ['timestamp' ]
170
+ if kwargs .__contains__ ('metadata' ):
171
+ logs ['metadata' ] = kwargs ['metadata' ]
159
172
body = []
160
173
body .append (logs )
161
174
return self .make_request (path = '/log/ingest' , method = 'POST' ,
0 commit comments