Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
20  mlog_libs/mlog.pm
@@ -93,6 +93,13 @@ use_api_log_level(string component) : Removes the user-defined log level for thi
93 93
 =cut
94 94
 
95 95
 sub init_mlog {
  96
+    %user_defined_log_levels = ();
  97
+    $msg_count = 0;
  98
+    _update_api_defined_log_levels();
  99
+}
  100
+
  101
+sub _update_api_defined_log_levels {
  102
+    %api_defined_log_levels = ();
96 103
     $last_update_msg_count = 0;
97 104
     $last_update_time = DateTime->now( time_zone => 'local' )->set_time_zone('floating');
98 105
 
@@ -196,33 +203,32 @@ sub logit {
196 203
     }
197 204
 
198 205
     if($msg_count == 0 && $last_update_time eq "") {
199  
-        print STDERR "WARNING: mlog_init() was not called, so I will call it for you.\n";
200  
-        mlog_init();
  206
+        print STDERR "WARNING: init_mlog() was not called, so I will call it for you.\n";
  207
+        init_mlog();
201 208
     }
202 209
 
203 210
     ++$msg_count;
204 211
     ++$last_update_msg_count;
205 212
 
206  
-    # May want to include these in 1st openlog argument
207 213
     my $user = $ENV{'USER'};
208 214
     my $ident = abs_path($0);
209 215
     my $logopt = "";
210 216
 
211 217
     if($last_update_msg_count >= $MSG_CHECK_COUNT || _get_time_since_start() >= $MSG_CHECK_INTERVAL) {
212  
-        mlog_init();
  218
+        init_mlog();
213 219
     }
214 220
 
215  
-    # If this message is an emergency, send a copy to the emergency facility
  221
+    # If this message is an emergency, send a copy to the emergency facility first.
216 222
     if($level == 0) {
217 223
         setlogsock('unix');
218  
-        openlog("$component:$user:$$:$ident:$error_code", $logopt, $EMERG_FACILITY);
  224
+        openlog("$component:$user:$error_code:$ident\[$$\]", $logopt, $EMERG_FACILITY);
219 225
         syslog($LOG_LEVEL_TEXT[$level], "$message");
220 226
         closelog();
221 227
     }
222 228
 
223 229
     if($level <= _get_log_level($component)) {
224 230
         setlogsock('unix');
225  
-        openlog("$component:$user:$$:$ident:$error_code", $logopt, $MSG_FACILITY);
  231
+        openlog("$component:$user:$error_code:$ident\[$$\]", $logopt, $MSG_FACILITY);
226 232
         syslog($LOG_LEVEL_TEXT[$level], "$message");
227 233
         closelog();
228 234
     } else {
50  mlog_libs/mlog.py
@@ -6,8 +6,6 @@
6 6
        A library for sending MG-RAST logging to syslog.
7 7
 
8 8
 METHODS
9  
-       init_mlog(): Initializes mlog. It's good to call this at the beginning of your program.
10  
-
11 9
        logit(level, component, message, error_code): sends mgrast log message to syslog.
12 10
 
13 11
        *         level: (0-6) The logging level for this message is compared to the logging level that has been set in
@@ -60,8 +58,6 @@
60 58
 import datetime, re
61 59
 import inspect, os, getpass, sys
62 60
 
63  
-#__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"]
64  
-
65 61
 MLOG_CONF_FILE = "/etc/mlog/mlog.conf"
66 62
 DEFAULT_LOG_LEVEL = 6
67 63
 MSG_CHECK_COUNT = 100
@@ -71,21 +67,22 @@
71 67
 LOG_LEVEL_TEXT = [ syslog.LOG_EMERG, syslog.LOG_ALERT, syslog.LOG_CRIT, syslog.LOG_ERR,
72 68
                    syslog.LOG_WARNING, syslog.LOG_NOTICE, syslog.LOG_INFO, syslog.LOG_DEBUG ]
73 69
 
74  
-api_defined_log_levels = {}
75  
-user_defined_log_levels = {}
76  
-msg_count = 0
77  
-last_update_time = ""
78  
-last_update_msg_count = 0
79  
-
80 70
 class mlog(object):
81 71
     """
82 72
     This class contains the methods necessary for sending MG-RAST log messages.
83 73
     """
84 74
 
85 75
     def __init__(self):
  76
+        self.user_defined_log_levels = {}
  77
+        self.msg_count = 0
  78
+        self._update_api_defined_log_levels()
  79
+
  80
+    def _update_api_defined_log_levels(self):
  81
+        self.api_defined_log_levels = {}
86 82
         self.last_update_msg_count = 0
87 83
         self.last_update_time = datetime.datetime.now()
88 84
 
  85
+        # Retrieving the control API defined log levels...
89 86
         api_mlog_url = ""
90 87
         for line in open(MLOG_CONF_FILE):
91 88
             line.strip()
@@ -105,13 +102,13 @@ def __init__(self):
105 102
                 print "         set_log_level(integer level, string component)"
106 103
             else:
107 104
                 for components in data['components']:
108  
-                    api_defined_log_levels[components['name']] = components['log_level']
  105
+                    self.api_defined_log_levels[components['name']] = components['log_level']
109 106
 
110 107
     def _get_log_level(self, component):
111  
-        if(component in api_defined_log_levels):
112  
-            return api_defined_log_levels[component]
113  
-        elif(component in user_defined_log_levels):
114  
-            return user_defined_log_levels[component]
  108
+        if(component in self.user_defined_log_levels):
  109
+            return self.user_defined_log_levels[component]
  110
+        elif(component in self.api_defined_log_levels):
  111
+            return self.api_defined_log_levels[component]
115 112
         else:
116 113
             return DEFAULT_LOG_LEVEL
117 114
 
@@ -123,7 +120,7 @@ def set_log_level(self, level, component):
123 120
         if(not isinstance(level, int) or component == ""):
124 121
             sys.stderr.write("ERROR: Format for calling set_log_level is set_log_level(integer level, string component)\n")
125 122
             return 0
126  
-        user_defined_log_levels[component] = level
  123
+        self.user_defined_log_levels[component] = level
127 124
         return 1
128 125
 
129 126
     def set_log_msg_check_count(self, count):
@@ -141,14 +138,14 @@ def set_log_msg_check_interval(self, interval):
141 138
         return 1
142 139
 
143 140
     def use_all_api_log_levels(self):
144  
-        user_defined_log_levels.clear()
  141
+        self.user_defined_log_levels.clear()
145 142
         return 1
146 143
 
147 144
     def use_api_log_level(self, component):
148 145
         if(component == ""):
149 146
             sys.stderr.write("ERROR: Format for calling use_api_log_level is use_api_log_level(string component)\n")
150 147
             return 0
151  
-        del user_defined_log_levels[component]
  148
+        del self.user_defined_log_levels[component]
152 149
         return 1
153 150
 
154 151
     def logit(self, level, component, message, error_code):
@@ -160,25 +157,26 @@ def logit(self, level, component, message, error_code):
160 157
             sys.stderr.write("ERROR: mlog level '"+level+"' is invalid, you must enter an integer between 0 and 6, inclusive.\n")
161 158
             return 0
162 159
 
163  
-        ++msg_count
164  
-        ++last_update_msg_count
  160
+        ++self.msg_count
  161
+        ++self.last_update_msg_count
165 162
 
166 163
         user = getpass.getuser()
167 164
         ident = os.path.abspath(inspect.getfile(inspect.stack()[1][0]))
168 165
         logopt = ""
169 166
 
170  
-        if(last_update_msg_count >= MSG_CHECK_COUNT or self._get_time_since_start() >= MSG_CHECK_INTERVAL):
171  
-            self.__init__()
  167
+        if(self.last_update_msg_count >= MSG_CHECK_COUNT or self._get_time_since_start() >= MSG_CHECK_INTERVAL):
  168
+            self._update_api_defined_log_levels()
172 169
 
  170
+        # If this message is an emergency, send a copy to the emergency facility first.
173 171
         if(level == 0):
174  
-            syslog.openlog(logoption=syslog.LOG_PID, facility=EMERG_FACILITY)
  172
+            syslog.openlog(component+":"+user+":"+error_code+":"+ident, syslog.LOG_PID, EMERG_FACILITY)
175 173
             syslog.syslog(syslog.LOG_EMERG, message)
176  
-            print component+":"+user+":process_id:"+ident+":"+error_code
  174
+            syslog.closelog()
177 175
 
178 176
         if(level <= self._get_log_level(component)):
179  
-            syslog.openlog(logoption=syslog.LOG_PID, facility=MSG_FACILITY)
  177
+            syslog.openlog(component+":"+user+":"+error_code+":"+ident, syslog.LOG_PID, MSG_FACILITY)
180 178
             syslog.syslog(LOG_LEVEL_TEXT[level], message)
181  
-            print component+":"+user+":process_id:"+ident+":"+error_code
  179
+            syslog.closelog()
182 180
         else:
183 181
             return 0
184 182
 
24  test_scripts/test_mlog_pm_lib.pl
... ...
@@ -0,0 +1,24 @@
  1
+#!/usr/bin/env perl
  2
+
  3
+use strict;
  4
+use mlog;
  5
+
  6
+init_mlog();
  7
+
  8
+if(logit(5, "mgrast", "my test message", "error code X")) {
  9
+  print "log message sent!\n";
  10
+} else {
  11
+  print "log message not sent.\n";
  12
+}
  13
+
  14
+if(set_log_level(5, "mgrast")) {
  15
+  print "log level changed!\n";
  16
+} else {
  17
+  print "log level not changed.\n";
  18
+}
  19
+
  20
+if(logit(5, "mgrast", "test_msg", "error code Y")) {
  21
+  print "log message sent!\n";
  22
+} else {
  23
+  print "log message not sent.\n";
  24
+}
32  test_scripts/test_mlog_py_lib.py
... ...
@@ -0,0 +1,32 @@
  1
+#!/soft/packages/python/2.6/bin/python
  2
+
  3
+import os
  4
+import sys
  5
+import re, time
  6
+
  7
+# Getting paths
  8
+MCP_path = os.path.realpath(__file__)
  9
+matchObj = re.match(r'(^.*\/)', MCP_path)
  10
+MCP_dir = matchObj.group(0)
  11
+
  12
+# Adding modules directory to path
  13
+path = list(sys.path)
  14
+sys.path.insert(0, MCP_dir+"mlog_libs/")
  15
+
  16
+from mlog import mlog
  17
+
  18
+m = mlog()
  19
+if(m.logit(5, 'mgrast', 'my test message', 'error code X')):
  20
+    print "log message sent!"
  21
+else:
  22
+    print "log message not sent."
  23
+
  24
+if(m.set_log_level(5, "mgrast")):
  25
+  print "log level changed!";
  26
+else:
  27
+  print "log level not changed.";
  28
+
  29
+if(m.logit(5, "mgrast", "test_msg", "error code Y")):
  30
+  print "log message sent!"
  31
+else:
  32
+  print "log message not sent."

No commit comments for this range

Something went wrong with that request. Please try again.