"""
MI logging can be configured using a combination of two of four files.
there is first a "base" configuration, and then a "local" set of overrides.

the base configuration is from the file specified in the environment variable MI_LOGGING_CONFIG
or res/config/mi-logging.yml (ie, users can set MI-specific configuration for drivers run from pycc container)
or config/logging.yml from within the MI egg (default to use if no mi-logging.yml was created)

then the local override may be res/config/mi-logging.local.yml (for overrides specific to MI),
or if this is not found, then res/config/logging.local.yml,
or if this is not found then no overrides.

The get_logger function is obsolete but kept to simplify transition to the ooi.logging code.

USAGE:
to configure logging from the standard MI configuration files:

    from mi.core.log import LoggerManager
    LoggerManager()

to create a logger automatically scoped with the calling package and ready to use:

    from ooi.logging import log    # no longer need get_logger at all

"""
import os.path
import pkg_resources

from mi.core.common import Singleton
from ooi.logging import config, log

LOGGING_CONFIG_ENVIRONMENT_VARIABLE="MI_LOGGING_CONFIG"

LOGGING_PRIMARY_FROM_FILE='res/config/mi-logging.yml'
LOGGING_PRIMARY_FROM_EGG='logging.yml'
LOGGING_MI_OVERRIDE='res/config/mi-logging.local.yml'
LOGGING_CONTAINER_OVERRIDE='res/config/logging.local.yml'

class LoggerManager(Singleton):
    """
    Logger Manager.  Provides an interface to configure logging at runtime.
    """
    def init(self):
        """Initialize logging for MI.  Because this is a singleton it will only be initialized once."""
        print 'starting log init'
        path = os.environ[LOGGING_CONFIG_ENVIRONMENT_VARIABLE] if LOGGING_CONFIG_ENVIRONMENT_VARIABLE in os.environ else None
        print 'env path %r' % path
        haveenv = path and os.path.isfile(path)
        print 'have env %r' % haveenv
        if path and not haveenv:
            print 'WARNING: %s was set but %s was not found (using default configuration files instead)' % (LOGGING_CONFIG_ENVIRONMENT_VARIABLE, path)
        if path and haveenv:
            print 'about to use env path'
            config.replace_configuration(path)
            print 'configured from ' + path
        elif os.path.isfile(LOGGING_PRIMARY_FROM_FILE):
            config.replace_configuration(LOGGING_PRIMARY_FROM_FILE)
            print 'configured from ' + LOGGING_PRIMARY_FROM_FILE
        else:
            path = pkg_resources.resource_filename('config', LOGGING_PRIMARY_FROM_EGG)
            config.replace_configuration(path)
            print 'configured from ' + path

        if os.path.isfile(LOGGING_MI_OVERRIDE):
            config.add_configuration(LOGGING_MI_OVERRIDE)
        elif os.path.isfile(LOGGING_CONTAINER_OVERRIDE):
            config.add_configuration(LOGGING_CONTAINER_OVERRIDE)

def get_logger():
    return log



@@ -1043,21 +1043,27 @@
 mport os
-.path
+%0Aimport sys
 %0Aimport 
@@ -1555,16 +1555,29 @@
 nit(self
+, debug=False
 ):%0A     
@@ -1679,42 +1679,8 @@
 %22%22%22%0A
-        print 'starting log init'%0A
     
@@ -1805,43 +1805,8 @@
 one%0A
-        print 'env path %25r' %25 path%0A
     
@@ -1853,46 +1853,8 @@
 th)%0A
-        print 'have env %25r' %25 haveenv%0A
     
@@ -1899,16 +1899,30 @@
    print
+ %3E%3E os.stderr,
  'WARNIN
@@ -2080,50 +2080,8 @@
 nv:%0A
-            print 'about to use env path'%0A
     
@@ -2127,39 +2127,100 @@
 th)%0A            
-print '
+if debug:%0A                print %3E%3E sys.stderr, str(os.getpid()) + ' 
 configured from 
@@ -2206,32 +2206,40 @@
  + ' configured 
+logging 
 from ' + path%0A  
@@ -2364,39 +2364,100 @@
 LE)%0A            
-print '
+if debug:%0A                print %3E%3E sys.stderr, str(os.getpid()) + ' 
 configured from 
@@ -2443,32 +2443,40 @@
  + ' configured 
+logging 
 from ' + LOGGING
@@ -2658,15 +2658,76 @@
     
-print '
+if debug:%0A                print %3E%3E sys.stderr, str(os.getpid()) + ' 
 conf
@@ -2733,16 +2733,24 @@
 figured 
+logging 
 from ' +
@@ -2874,121 +2874,351 @@
     
-elif os.path.isfile(LOGGING_CONTAINER_OVERRIDE):%0A            config.add_configuration(LOGGING_CONTAINER_OVERRIDE)
+    if debug:%0A                print %3E%3E sys.stderr, str(os.getpid()) + ' supplemented logging from ' + path%0A        elif os.path.isfile(LOGGING_CONTAINER_OVERRIDE):%0A            config.add_configuration(LOGGING_CONTAINER_OVERRIDE)%0A            if debug:%0A                print %3E%3E sys.stderr, str(os.getpid()) + ' supplemented logging from ' + path
 %0A%0Ade

make logging debug print statements conditional