<s> from __future__ import print_function , unicode_literals , division , absolute_import <EOL> import socket , json , time , os , logging <EOL> from logging . handlers import SysLogHandler <EOL> from dxpy . exceptions import DXError <EOL> class DXLogHandler ( SysLogHandler ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , priority_log_address = "<STR_LIT>" , <EOL> bulk_log_address = "<STR_LIT>" , <EOL> source = "<STR_LIT>" ) : <EOL> logging . Handler . __init__ ( self ) <EOL> self . priority_log_address = priority_log_address <EOL> self . priority_log_socket = socket . socket ( socket . AF_UNIX , socket . SOCK_DGRAM ) <EOL> self . bulk_log_address = bulk_log_address <EOL> self . bulk_log_socket = socket . socket ( socket . AF_UNIX , socket . SOCK_DGRAM ) <EOL> if not os . path . exists ( priority_log_address ) : <EOL> raise DXError ( "<STR_LIT>" % ( priority_log_address ) ) <EOL> if not os . path . exists ( bulk_log_address ) : <EOL> raise DXError ( "<STR_LIT>" % ( bulk_log_address ) ) <EOL> self . priority_log_socket . connect ( priority_log_address ) <EOL> self . bulk_log_socket . connect ( bulk_log_address ) <EOL> self . source = source <EOL> def close ( self ) : <EOL> self . priority_log_socket . close ( ) <EOL> self . bulk_log_socket . close ( ) <EOL> logging . Handler . close ( self ) <EOL> def encodePriority ( self , record ) : <EOL> return self . priority_names [ self . priority_map . get ( record . levelname , "<STR_LIT>" ) ] <EOL> def emit ( self , record ) : <EOL> level = self . encodePriority ( record ) <EOL> message = record . getMessage ( ) <EOL> if len ( message ) > <NUM_LIT> : <EOL> message = message [ : <NUM_LIT> ] + "<STR_LIT>" <EOL> data = json . dumps ( { "<STR_LIT:source>" : self . source , "<STR_LIT>" : int ( round ( time . time ( ) * <NUM_LIT:1000> ) ) , <EOL> "<STR_LIT>" : level , "<STR_LIT>" : message } ) <EOL> levelno = int ( record . levelno ) <EOL> if levelno >= logging . CRITICAL or ( levelno == logging . INFO and message . startswith ( b"<STR_LIT>" ) ) : <EOL> cur_socket = self . priority_log_socket <EOL> cur_socket_address = self . priority_log_address <EOL> else : <EOL> cur_socket = self . bulk_log_socket <EOL> cur_socket_address = self . bulk_log_address <EOL> try : <EOL> cur_socket . send ( data ) <EOL> except socket . error : <EOL> cur_socket . connect ( cur_socket_address ) <EOL> cur_socket . send ( data ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> self . handleError ( record ) </s>
