Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
a5c08f17
Commit
a5c08f17
authored
12 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
logging better documented + JSON serialization
parent
66d6602a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
instrumentation/logging/logentry.py
+44
-3
44 additions, 3 deletions
instrumentation/logging/logentry.py
instrumentation/unittests/logging.py
+8
-0
8 additions, 0 deletions
instrumentation/unittests/logging.py
with
52 additions
and
3 deletions
instrumentation/logging/logentry.py
+
44
−
3
View file @
a5c08f17
import
time
import
json
import
cPickle
as
pickle
import
base64
class
LogEntry
(
object
):
"""
Class that represents a single log entry in the system
"""
"""
Class that represents a single log entry in the system.
Basic log has at least a timestamp, information who logged the event, a set of
tags that designate severity or classification. A log entry can also have a
'
main
attachment
'
, which can be a number, string, sequence of those objects or a dictionary
of those objects. Main attachment serves as additional source of information that
can be deployed for indexing the log entries.
A log entry can additionally have one or more named attachments. There can be at
most one attachment with given name. Those are serializes and are not expected to be
indexable during log retrieval.
"""
def
__init__
(
self
,
who
,
tags
,
when
=
None
):
"""
@param who: Name of the logging service. System components in granularity-descending
...
...
@@ -29,7 +44,7 @@ class LogEntry(object):
"""
Attaches a piece of data to the log entry.
Invoke with either one argument (will attach the data as main attachment) or two arguments
(first of them will be a str, name of the entry, second one - the data to attach)
(first of them will be a str, name of the entry, second one - the data to attach)
.
"""
if
len
(
args
)
==
1
:
# Attach an attachment without a name
self
.
main_attachment
=
args
[
0
]
...
...
@@ -38,4 +53,30 @@ class LogEntry(object):
else
:
raise
ValueError
,
'
more than 2 arguments
'
return
self
\ No newline at end of file
return
self
def
to_JSON
(
self
):
"""
Serializes this object to JSON.
"""
return
json
.
dumps
({
'
when
'
:
self
.
when
,
'
who
'
:
self
.
who
,
'
tags
'
:
sorted
(
self
.
tags
),
'
main
'
:
self
.
main_attachment
,
'
attachments
'
:
dict
((
(
name
,
base64
.
b64encode
(
pickle
.
dumps
(
value
,
pickle
.
HIGHEST_PROTOCOL
)))
for
name
,
value
in
self
.
attachments
.
iteritems
()
))
})
@staticmethod
def
from_JSON
(
jsonstr
):
"""
Unserializes this object from JSON. This may be potentially
unsafe, as we are unpickling Python objects.
Know that main_attachment
'
s str
'
s will get converted to Unicode, due to how JSON works.
@type jsonstr: str
"""
jo
=
json
.
loads
(
jsonstr
)
le
=
LogEntry
(
str
(
jo
[
'
who
'
]),
map
(
str
,
jo
[
'
tags
'
]),
jo
[
'
when
'
]).
attach
(
jo
[
'
main
'
])
for
aname
,
avs
in
jo
[
'
attachments
'
].
iteritems
():
le
.
attach
(
aname
,
pickle
.
loads
(
base64
.
b64decode
(
avs
)))
return
le
This diff is collapsed.
Click to expand it.
instrumentation/unittests/logging.py
+
8
−
0
View file @
a5c08f17
...
...
@@ -4,6 +4,14 @@ from satella.instrumentation.logging import LogEntry, LogSet, LoggerInterface
import
unittest
class
LogsetTest
(
unittest
.
TestCase
):
def
test_serialization
(
self
):
k
=
LogEntry
(
'
a.b
'
,
'
a b
'
).
attach
(
'
stefan
'
,
'
nope
'
)
k
=
LogEntry
.
from_JSON
(
k
.
to_JSON
())
self
.
assertEquals
(
k
.
attachments
[
'
stefan
'
],
'
nope
'
)
def
test_logset_filtering
(
self
):
f
=
[
LogEntry
(
'
x.y.z.a
'
,
'
satella test
'
,
1
),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment