Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PyPCC
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LOFAR2.0
PyPCC
Commits
5befa504
Commit
5befa504
authored
3 years ago
by
Jan David Mol
Browse files
Options
Downloads
Patches
Plain Diff
Allow logs to also be send to a logstash server (f.e. the LCU)
parent
a425bb6e
No related branches found
No related tags found
1 merge request
!14
Log to LCU
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
logconfig.py
+54
-0
54 additions, 0 deletions
logconfig.py
pypcc2.py
+10
-6
10 additions, 6 deletions
pypcc2.py
requirements.txt
+1
-0
1 addition, 0 deletions
requirements.txt
with
65 additions
and
6 deletions
logconfig.py
0 → 100644
+
54
−
0
View file @
5befa504
import
logging
logger
=
logging
.
getLogger
()
def
configure_logger
(
logstash_host
:
str
=
""
,
level
=
logging
.
DEBUG
,
log_extra
:
dict
=
None
):
"""
Configure the output of Python
'
s logging module.
logstash_host: Logstash server to forward a copy of the logs to (f.e. the LCU).
level: Configure to use this log level (can be a string, or integer)
log_extra: Extra dict to annotate log lines with, or None.
"""
# By default we want to know everything
if
isinstance
(
level
,
str
):
level_nr
=
getattr
(
logging
,
level
.
upper
(),
None
)
if
not
isinstance
(
level_nr
,
int
):
raise
ValueError
(
'
Invalid log level: %s
'
%
level
)
logger
.
setLevel
(
level_nr
)
else
:
logger
.
setLevel
(
level
)
# remove spam from various libraries
logging
.
getLogger
(
"
asyncua
"
).
setLevel
(
logging
.
WARN
)
logging
.
getLogger
(
"
opcua
"
).
setLevel
(
logging
.
WARN
)
logging
.
getLogger
(
"
git
"
).
setLevel
(
logging
.
WARN
)
# log to stderr, in a way that it can be understood by a human reader
handler
=
logging
.
StreamHandler
()
formatter
=
logging
.
Formatter
(
fmt
=
'
%(asctime)s [%(levelname)-8s,%(filename)-20s:%(lineno)-3d] %(message)s
'
,
datefmt
=
'
%Y-%m-%dT%H:%M:%S
'
)
handler
.
setFormatter
(
formatter
)
logger
.
addHandler
(
handler
)
# Log to ELK stack
if
logstash_host
:
try
:
from
logstash_async.handler
import
AsynchronousLogstashHandler
,
LogstashFormatter
logger
.
info
(
f
"
Sending logs to Logstash at
{
logstash_host
}
"
)
# log to the tcp_input of logstash, cache pending messages if we cannot reach the host
handler
=
AsynchronousLogstashHandler
(
logstash_host
,
5959
,
database_path
=
'
pending_log_messages.db
'
)
# configure log messages
formatter
=
LogstashFormatter
(
extra
=
log_extra
,
tags
=
[
"
python
"
,
"
lofar
"
,
"
pypcc
"
])
handler
.
setFormatter
(
formatter
)
# install the handler
logger
.
addHandler
(
handler
)
except
ImportError
:
logger
.
exception
(
"
Cannot send logs to Logstash: logstash_async module not found.
"
)
except
Exception
:
logger
.
exception
(
f
"
Cannot send logs to Logstash at
{
logstash_host
}
"
)
This diff is collapsed.
Click to expand it.
pypcc2.py
+
10
−
6
View file @
5befa504
...
...
@@ -16,15 +16,19 @@ parser.add_argument("-t", "--test", help="Do not start OPC-UA server.", action="
parser
.
add_argument
(
"
-p
"
,
"
--port
"
,
help
=
"
Port number to listen on [%(default)s].
"
,
type
=
int
,
default
=
4842
)
parser
.
add_argument
(
"
-l
"
,
"
--loglevel
"
,
help
=
"
Log level [%(default)s].
"
,
type
=
str
,
choices
=
[
"
DEBUG
"
,
"
INFO
"
,
"
WARNING
"
,
"
ERROR
"
],
default
=
"
INFO
"
)
parser
.
add_argument
(
"
-c
"
,
"
--config
"
,
help
=
"
YAML config files, comma seperated [%(default)s]
"
,
type
=
str
,
default
=
'
RCU
'
)
parser
.
add_argument
(
"
--loghost
"
,
help
=
"
Logstash host to which to forward logs [%(default)s]
"
,
type
=
str
,
default
=
''
)
args
=
parser
.
parse_args
()
# set log level
loglevel_nr
=
getattr
(
logging
,
args
.
loglevel
.
upper
(),
None
)
if
not
isinstance
(
loglevel_nr
,
int
):
raise
ValueError
(
'
Invalid log level: %s
'
%
args
.
loglevel
)
#logging.basicConfig(level=loglevel_nr, format="%(asctime)s [%(levelname)8s] %(message)s")
from
logconfig
import
configure_logger
log_extra
=
{
"
simulator
"
:
args
.
simulator
,
"
test
"
:
args
.
test
,
"
port
"
:
args
.
port
,
"
config
"
:
args
.
config
,
"
lofar_id
"
:
f
"
pypcc -
{
args
.
config
}
"
,
}
configure_logger
(
logstash_host
=
args
.
loghost
,
level
=
args
.
loglevel
,
log_extra
=
log_extra
)
logging
.
basicConfig
(
level
=
loglevel_nr
,
format
=
'
%(asctime)s [%(levelname)-8s,%(filename)-20s:%(lineno)-3d] %(message)s
'
)
RunTimer
=
True
;
def
signal_handler
(
sig
,
frame
):
logging
.
warn
(
'
Stop signal received!
'
)
...
...
This diff is collapsed.
Click to expand it.
requirements.txt
+
1
−
0
View file @
5befa504
...
...
@@ -3,3 +3,4 @@ opcua
numpy
recordclass
pyyaml
python-logstash-async
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