forked from AlexIoannides/pyspark-example-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.py
51 lines (39 loc) · 1.23 KB
/
logging.py
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
"""
logging
~~~~~~~
This module contains a class that wraps the log4j object instantiated
by the active SparkContext, enabling Log4j logging for PySpark using.
"""
class Log4j(object):
"""Wrapper class for Log4j JVM object.
:param spark: SparkSession object.
"""
def __init__(self, spark):
# get spark app details with which to prefix all messages
conf = spark.sparkContext.getConf()
app_id = conf.get('spark.app.id')
app_name = conf.get('spark.app.name')
log4j = spark._jvm.org.apache.log4j
message_prefix = '<' + app_name + ' ' + app_id + '>'
self.logger = log4j.LogManager.getLogger(message_prefix)
def error(self, message):
"""Log an error.
:param: Error message to write to log
:return: None
"""
self.logger.error(message)
return None
def warn(self, message):
"""Log a warning.
:param: Warning message to write to log
:return: None
"""
self.logger.warn(message)
return None
def info(self, message):
"""Log information.
:param: Information message to write to log
:return: None
"""
self.logger.info(message)
return None