Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

mlog libraries are ready for testing.

  • Loading branch information...
commit 4d3ddeba5210212b5a8c1a7a813644c983f98d0b 1 parent 81a511e
@jaredbischof jaredbischof authored
Showing with 37 additions and 33 deletions.
  1. +13 −7 mlog_libs/mlog.pm
  2. +24 −26 mlog_libs/mlog.py
View
20 mlog_libs/mlog.pm
@@ -93,6 +93,13 @@ use_api_log_level(string component) : Removes the user-defined log level for thi
=cut
sub init_mlog {
+ %user_defined_log_levels = ();
+ $msg_count = 0;
+ _update_api_defined_log_levels();
+}
+
+sub _update_api_defined_log_levels {
+ %api_defined_log_levels = ();
$last_update_msg_count = 0;
$last_update_time = DateTime->now( time_zone => 'local' )->set_time_zone('floating');
@@ -196,33 +203,32 @@ sub logit {
}
if($msg_count == 0 && $last_update_time eq "") {
- print STDERR "WARNING: mlog_init() was not called, so I will call it for you.\n";
- mlog_init();
+ print STDERR "WARNING: init_mlog() was not called, so I will call it for you.\n";
+ init_mlog();
}
++$msg_count;
++$last_update_msg_count;
- # May want to include these in 1st openlog argument
my $user = $ENV{'USER'};
my $ident = abs_path($0);
my $logopt = "";
if($last_update_msg_count >= $MSG_CHECK_COUNT || _get_time_since_start() >= $MSG_CHECK_INTERVAL) {
- mlog_init();
+ init_mlog();
}
- # If this message is an emergency, send a copy to the emergency facility
+ # If this message is an emergency, send a copy to the emergency facility first.
if($level == 0) {
setlogsock('unix');
- openlog("$component:$user:$$:$ident:$error_code", $logopt, $EMERG_FACILITY);
+ openlog("$component:$user:$error_code:$ident\[$$\]", $logopt, $EMERG_FACILITY);
syslog($LOG_LEVEL_TEXT[$level], "$message");
closelog();
}
if($level <= _get_log_level($component)) {
setlogsock('unix');
- openlog("$component:$user:$$:$ident:$error_code", $logopt, $MSG_FACILITY);
+ openlog("$component:$user:$error_code:$ident\[$$\]", $logopt, $MSG_FACILITY);
syslog($LOG_LEVEL_TEXT[$level], "$message");
closelog();
} else {
View
50 mlog_libs/mlog.py
@@ -6,8 +6,6 @@
A library for sending MG-RAST logging to syslog.
METHODS
- init_mlog(): Initializes mlog. It's good to call this at the beginning of your program.
-
logit(level, component, message, error_code): sends mgrast log message to syslog.
* level: (0-6) The logging level for this message is compared to the logging level that has been set in
@@ -60,8 +58,6 @@
import datetime, re
import inspect, os, getpass, sys
-#__all__ = ["mlog", "set_log_level", "set_log_msg_check_count", "set_log_msg_check_interval", "logit", "use_all_api_log_levels", "use_api_log_level"]
-
MLOG_CONF_FILE = "/etc/mlog/mlog.conf"
DEFAULT_LOG_LEVEL = 6
MSG_CHECK_COUNT = 100
@@ -71,21 +67,22 @@
LOG_LEVEL_TEXT = [ syslog.LOG_EMERG, syslog.LOG_ALERT, syslog.LOG_CRIT, syslog.LOG_ERR,
syslog.LOG_WARNING, syslog.LOG_NOTICE, syslog.LOG_INFO, syslog.LOG_DEBUG ]
-api_defined_log_levels = {}
-user_defined_log_levels = {}
-msg_count = 0
-last_update_time = ""
-last_update_msg_count = 0
-
class mlog(object):
"""
This class contains the methods necessary for sending MG-RAST log messages.
"""
def __init__(self):
+ self.user_defined_log_levels = {}
+ self.msg_count = 0
+ self._update_api_defined_log_levels()
+
+ def _update_api_defined_log_levels(self):
+ self.api_defined_log_levels = {}
self.last_update_msg_count = 0
self.last_update_time = datetime.datetime.now()
+ # Retrieving the control API defined log levels...
api_mlog_url = ""
for line in open(MLOG_CONF_FILE):
line.strip()
@@ -105,13 +102,13 @@ def __init__(self):
print " set_log_level(integer level, string component)"
else:
for components in data['components']:
- api_defined_log_levels[components['name']] = components['log_level']
+ self.api_defined_log_levels[components['name']] = components['log_level']
def _get_log_level(self, component):
- if(component in api_defined_log_levels):
- return api_defined_log_levels[component]
- elif(component in user_defined_log_levels):
- return user_defined_log_levels[component]
+ if(component in self.user_defined_log_levels):
+ return self.user_defined_log_levels[component]
+ elif(component in self.api_defined_log_levels):
+ return self.api_defined_log_levels[component]
else:
return DEFAULT_LOG_LEVEL
@@ -123,7 +120,7 @@ def set_log_level(self, level, component):
if(not isinstance(level, int) or component == ""):
sys.stderr.write("ERROR: Format for calling set_log_level is set_log_level(integer level, string component)\n")
return 0
- user_defined_log_levels[component] = level
+ self.user_defined_log_levels[component] = level
return 1
def set_log_msg_check_count(self, count):
@@ -141,14 +138,14 @@ def set_log_msg_check_interval(self, interval):
return 1
def use_all_api_log_levels(self):
- user_defined_log_levels.clear()
+ self.user_defined_log_levels.clear()
return 1
def use_api_log_level(self, component):
if(component == ""):
sys.stderr.write("ERROR: Format for calling use_api_log_level is use_api_log_level(string component)\n")
return 0
- del user_defined_log_levels[component]
+ del self.user_defined_log_levels[component]
return 1
def logit(self, level, component, message, error_code):
@@ -160,25 +157,26 @@ def logit(self, level, component, message, error_code):
sys.stderr.write("ERROR: mlog level '"+level+"' is invalid, you must enter an integer between 0 and 6, inclusive.\n")
return 0
- ++msg_count
- ++last_update_msg_count
+ ++self.msg_count
+ ++self.last_update_msg_count
user = getpass.getuser()
ident = os.path.abspath(inspect.getfile(inspect.stack()[1][0]))
logopt = ""
- if(last_update_msg_count >= MSG_CHECK_COUNT or self._get_time_since_start() >= MSG_CHECK_INTERVAL):
- self.__init__()
+ if(self.last_update_msg_count >= MSG_CHECK_COUNT or self._get_time_since_start() >= MSG_CHECK_INTERVAL):
+ self._update_api_defined_log_levels()
+ # If this message is an emergency, send a copy to the emergency facility first.
if(level == 0):
- syslog.openlog(logoption=syslog.LOG_PID, facility=EMERG_FACILITY)
+ syslog.openlog(component+":"+user+":"+error_code+":"+ident, syslog.LOG_PID, EMERG_FACILITY)
syslog.syslog(syslog.LOG_EMERG, message)
- print component+":"+user+":process_id:"+ident+":"+error_code
+ syslog.closelog()
if(level <= self._get_log_level(component)):
- syslog.openlog(logoption=syslog.LOG_PID, facility=MSG_FACILITY)
+ syslog.openlog(component+":"+user+":"+error_code+":"+ident, syslog.LOG_PID, MSG_FACILITY)
syslog.syslog(LOG_LEVEL_TEXT[level], message)
- print component+":"+user+":process_id:"+ident+":"+error_code
+ syslog.closelog()
else:
return 0
Please sign in to comment.
Something went wrong with that request. Please try again.