瀏覽代碼

AMBARI-18273. Add logging to differentiate between ambari-server start/restart commands vs. other commands.(vbrodetskyi)

Vitaly Brodetskyi 9 年之前
父節點
當前提交
877077a639

文件差異過大導致無法顯示
+ 0 - 0
ambari-server/conf/unix/ambari.properties


+ 1 - 2
ambari-server/sbin/ambari-server

@@ -99,8 +99,7 @@ case "$1" in
         ;;
   restart)
         echo -e "Restarting ambari-server"
-        $0 stop
-        $0 start
+        $PYTHON "$AMBARI_PYTHON_EXECUTABLE" $@
         ;;
   upgrade)
         echo -e "Upgrading ambari-server"

+ 64 - 2
ambari-server/src/main/python/ambari-server.py

@@ -22,6 +22,9 @@ import optparse
 import sys
 import os
 import signal
+import logging
+import logging.handlers
+import logging.config
 
 from ambari_commons.exceptions import FatalException, NonFatalException
 from ambari_commons.logging_utils import set_verbose, set_silent, \
@@ -46,8 +49,8 @@ from ambari_server.enableStack import enable_stack_version
 
 from ambari_server.setupActions import BACKUP_ACTION, LDAP_SETUP_ACTION, LDAP_SYNC_ACTION, PSTART_ACTION, \
   REFRESH_STACK_HASH_ACTION, RESET_ACTION, RESTORE_ACTION, UPDATE_HOST_NAMES_ACTION, CHECK_DATABASE_ACTION, \
-  SETUP_ACTION, SETUP_SECURITY_ACTION,START_ACTION, STATUS_ACTION, STOP_ACTION, UPGRADE_ACTION, UPGRADE_STACK_ACTION, \
-  SETUP_JCE_ACTION, SET_CURRENT_ACTION, START_ACTION, STATUS_ACTION, STOP_ACTION, UPGRADE_ACTION, \
+  SETUP_ACTION, SETUP_SECURITY_ACTION,START_ACTION, STATUS_ACTION, STOP_ACTION, RESTART_ACTION, UPGRADE_ACTION, \
+  UPGRADE_STACK_ACTION, SETUP_JCE_ACTION, SET_CURRENT_ACTION, START_ACTION, STATUS_ACTION, STOP_ACTION, UPGRADE_ACTION, \
   UPGRADE_STACK_ACTION, SETUP_JCE_ACTION, SET_CURRENT_ACTION, ENABLE_STACK_ACTION, SETUP_SSO_ACTION, \
   DB_CLEANUP_ACTION, INSTALL_MPACK_ACTION, UPGRADE_MPACK_ACTION
 from ambari_server.setupSecurity import setup_ldap, sync_ldap, setup_master_key, setup_ambari_krb5_jaas
@@ -56,6 +59,9 @@ from ambari_server.userInput import get_validated_string_input
 from ambari_server_main import server_process_main
 from ambari_server.ambariPath import AmbariPath
 
+logger = logging.getLogger()
+
+formatstr = "%(levelname)s %(asctime)s %(filename)s:%(lineno)d - %(message)s"
 
 class UserActionPossibleArgs(object):
   def __init__(self, i_fn, i_possible_args_numbers, *args, **kwargs):
@@ -111,12 +117,14 @@ def start(options):
 #
 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)
 def start(args):
+  logger.info("Starting ambari-server.")
   status, pid = is_server_runing()
   if status:
     err = "Ambari Server is already running."
     raise FatalException(1, err)
 
   server_process_main(args)
+  logger.info("Started ambari-server.")
 
 
 #
@@ -146,6 +154,7 @@ def stop():
 #
 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)
 def stop(args):
+  logger.info("Stopping ambari-server.")
   if (args != None):
     args.exit_message = None
 
@@ -160,8 +169,21 @@ def stop(args):
     pid_file_path = os.path.join(configDefaults.PID_DIR, PID_NAME)
     os.remove(pid_file_path)
     print "Ambari Server stopped"
+    logger.info("Ambari Server stopped")
   else:
     print "Ambari Server is not running"
+    logger.info("Ambari Server is not running")
+
+
+#
+# Restarts the Ambari Server.
+#
+@OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)
+def restart(args):
+  logger.info("Restarting ambari-server.")
+  stop(args)
+  start(args)
+
 
 
 #
@@ -184,6 +206,7 @@ def status(args):
 #
 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)
 def status(args):
+  logger.info("Get status of ambari-server.")
   args.exit_message = None
   status, pid = is_server_runing()
   pid_file_path = os.path.join(configDefaults.PID_DIR, PID_NAME)
@@ -197,6 +220,7 @@ def status(args):
 
 
 def refresh_stack_hash_action():
+  logger.info("Refresh stack hash.")
   properties = get_ambari_properties()
   refresh_stack_hash(properties)
 
@@ -224,6 +248,7 @@ def create_setup_security_actions(args):
   return action_list
 
 def setup_security(args):
+  logger.info("Setup security.")
   actions = create_setup_security_actions(args)
   choice = None
   if args.security_option is not None:
@@ -269,6 +294,7 @@ def get_backup_path(args):
   return path
 
 def backup(args):
+  logger.info("Backup.")
   print "Backup requested."
   backup_command = ["BackupRestore", 'backup']
   path = get_backup_path(args)
@@ -278,6 +304,7 @@ def backup(args):
   BackupRestore_main(backup_command)
 
 def restore(args):
+  logger.info("Restore.")
   print "Restore requested."
   restore_command = ["BackupRestore", 'restore']
   path = get_backup_path(args)
@@ -602,6 +629,7 @@ def create_user_action_map(args, options):
         SETUP_JCE_ACTION : UserActionPossibleArgs(setup_jce_policy, [2], args),
         START_ACTION: UserAction(start, options),
         STOP_ACTION: UserAction(stop, options),
+        RESTART_ACTION: UserAction(restart, options),
         RESET_ACTION: UserAction(reset, options),
         STATUS_ACTION: UserAction(status, options),
         UPGRADE_ACTION: UserAction(upgrade, options),
@@ -624,10 +652,42 @@ def create_user_action_map(args, options):
   return action_map
 
 
+def setup_logging(logger, filename, logging_level):
+  formatter = logging.Formatter(formatstr)
+  rotateLog = logging.handlers.RotatingFileHandler(filename, "a", 10000000, 25)
+  rotateLog.setFormatter(formatter)
+  logger.addHandler(rotateLog)
+
+  logging.basicConfig(format=formatstr, level=logging_level, filename=filename)
+  logger.setLevel(logging_level)
+  logger.info("loglevel=logging.{0}".format(logging._levelNames[logging_level]))
+
 #
 # Main.
 #
 def main(options, args, parser):
+  # init logger
+  properties = get_ambari_properties()
+  python_log_level = logging.INFO
+  python_log_name = "ambari-server-command.log"
+
+  custom_log_level = properties["server.python.log.level"]
+
+  if custom_log_level:
+    if custom_log_level == "INFO":
+      python_log_level = logging.INFO
+    if custom_log_level == "DEBUG":
+      python_log_level = logging.DEBUG
+
+  custom_log_name = properties["server.python.log.name"]
+
+  if custom_log_name:
+    python_log_name = custom_log_name
+
+  python_log = os.path.join(configDefaults.OUT_DIR, python_log_name)
+
+  setup_logging(logger, python_log, python_log_level)
+
   # set silent
   set_silent(options.silent)
 
@@ -692,6 +752,7 @@ def main(options, args, parser):
   except FatalException as e:
     if e.reason is not None:
       print_error_msg("Exiting with exit code {0}. \nREASON: {1}".format(e.code, e.reason))
+      logger.exception(str(e))
     sys.exit(e.code)
   except NonFatalException as e:
     options.exit_message = "Ambari Server '%s' completed with warnings." % action
@@ -733,6 +794,7 @@ def mainBody():
 
 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)
 def enable_stack(options, args):
+  logger.info("Enable stack.")
   if options.stack_name == None:
      print_error_msg ("Please provide stack name using --stack option")
      return -1

+ 4 - 1
ambari-server/src/main/python/ambari_server/checkDatabase.py

@@ -20,6 +20,7 @@ limitations under the License.
 
 import os
 import sys
+import logging
 
 from ambari_commons.exceptions import FatalException
 from ambari_server import serverConfiguration
@@ -38,11 +39,13 @@ from ambari_commons.os_utils import run_os_command
 from ambari_server.serverUtils import is_server_runing
 from ambari_server.userInput import get_YN_input
 
+logger = logging.getLogger(__name__)
+
 CHECK_DATABASE_HELPER_CMD = "{0} -cp {1} " + \
                          "org.apache.ambari.server.checks.DatabaseConsistencyChecker"
 
 def check_database(options):
-
+  logger.info("Check database consistency.")
   jdk_path = serverConfiguration.get_java_exe_path()
 
   if jdk_path is None:

+ 4 - 0
ambari-server/src/main/python/ambari_server/dbCleanup.py

@@ -28,6 +28,9 @@ from ambari_server.userInput import get_YN_input
 from ambari_server.serverClassPath import ServerClassPath
 from ambari_server.serverUtils import is_server_runing
 import datetime
+import logging
+
+logger = logging.getLogger(__name__)
 
 DB_CLEANUP_CMD = "{0} -cp {1} org.apache.ambari.server.cleanup.CleanupDriver --cluster-name {2} --from-date {3}> " + configDefaults.SERVER_OUT_FILE + " 2>&1"
 
@@ -98,6 +101,7 @@ def run_db_cleanup(options):
 # Database cleanup
 #
 def db_cleanup(options):
+    logger.info("Database cleanup.")
     return run_db_cleanup(options)
 
 

+ 4 - 0
ambari-server/src/main/python/ambari_server/hostUpdate.py

@@ -20,6 +20,7 @@ limitations under the License.
 
 import os
 import sys
+import logging
 
 from ambari_commons.exceptions import FatalException
 from ambari_server import serverConfiguration
@@ -38,11 +39,14 @@ from ambari_commons.os_utils import run_os_command
 from ambari_server.serverUtils import is_server_runing
 from ambari_server.userInput import get_YN_input
 
+logger = logging.getLogger(__name__)
+
 HOST_UPDATE_HELPER_CMD = "{0} -cp {1} " + \
                             "org.apache.ambari.server.update.HostUpdateHelper {2}" + \
                             " > " + configDefaults.SERVER_OUT_FILE + " 2>&1"
 
 def update_host_names(args, options):
+  logger.info("Update host names.")
   services_stopped = userInput.get_YN_input("Please, confirm Ambari services are stopped [y/n] (n)? ", False)
   if not services_stopped:
     print 'Exiting...'

+ 6 - 0
ambari-server/src/main/python/ambari_server/serverSetup.py

@@ -25,6 +25,7 @@ import shutil
 import sys
 import subprocess
 import getpass
+import logging
 
 from ambari_commons.exceptions import FatalException
 from ambari_commons.firewall import Firewall
@@ -50,6 +51,8 @@ from ambari_server.ambariPath import AmbariPath
 
 from ambari_commons.constants import AMBARI_SUDO_BINARY
 
+logger = logging.getLogger(__name__)
+
 # selinux commands
 GET_SE_LINUX_ST_CMD = locate_file('sestatus', '/usr/sbin')
 SE_SETENFORCE_CMD = "setenforce 0"
@@ -1079,6 +1082,7 @@ def check_setup_already_done():
 # Setup the Ambari Server.
 #
 def setup(options):
+  logger.info("Setup ambari-server.")
   if options.only_silent:
     if check_setup_already_done():
       print "Nothing was done. Ambari Setup already performed and cannot re-run setup in silent mode. Use \"ambari-server setup\" command without -s option to change Ambari setup."
@@ -1151,6 +1155,7 @@ def setup(options):
 # Setup the JCE policy for Ambari Server.
 #
 def setup_jce_policy(args):
+  logger.info("Setup JCE policy for ambari-server.")
   if not os.path.exists(args[1]):
     err = "Can not run 'setup-jce'. Invalid path {0}.".format(args[1])
     raise FatalException(1, err)
@@ -1193,6 +1198,7 @@ def setup_jce_policy(args):
 # Resets the Ambari Server.
 #
 def reset(options):
+  logger.info("Reset ambari-server.")
   if not is_root():
     err = configDefaults.MESSAGE_ERROR_RESET_NOT_ROOT
     raise FatalException(4, err)

+ 6 - 0
ambari-server/src/main/python/ambari_server/serverUpgrade.py

@@ -27,6 +27,7 @@ import urllib2
 import re
 import glob
 import optparse
+import logging
 
 from ambari_commons.exceptions import FatalException
 from ambari_commons.logging_utils import print_info_msg, print_warning_msg, print_error_msg, get_verbose
@@ -50,6 +51,8 @@ from ambari_server.serverClassPath import ServerClassPath
 from ambari_server.setupMpacks import replay_mpack_logs
 from ambari_commons.logging_utils import get_debug_mode,   set_debug_mode_from_options
 
+logger = logging.getLogger(__name__)
+
 # constants
 STACK_NAME_VER_SEP = "-"
 
@@ -79,6 +82,7 @@ SUSPEND_START_MODE = False
 #
 
 def upgrade_stack(args):
+  logger.info("Upgrade stack.")
   if not is_root():
     err = 'Ambari-server upgradestack should be run with ' \
           'root-level privileges'
@@ -331,6 +335,7 @@ def move_user_custom_actions():
     raise FatalException(1, err)
 
 def upgrade(args):
+  logger.info("Upgrade ambari-server.")
   if not is_root():
     err = configDefaults.MESSAGE_ERROR_UPGRADE_NOT_ROOT
     raise FatalException(4, err)
@@ -438,6 +443,7 @@ def add_jdbc_properties(properties):
 # Set current cluster version (run Finalize during manual RU)
 #
 def set_current(options):
+  logger.info("Set current cluster version.")
   server_status, pid = is_server_runing()
   if not server_status:
     err = 'Ambari Server is not running.'

+ 1 - 0
ambari-server/src/main/python/ambari_server/setupActions.py

@@ -23,6 +23,7 @@ SETUP_ACTION = "setup"
 START_ACTION = "start"
 PSTART_ACTION = "pstart"
 STOP_ACTION = "stop"
+RESTART_ACTION = "restart"
 RESET_ACTION = "reset"
 UPGRADE_ACTION = "upgrade"
 UPGRADE_STACK_ACTION = "upgradestack"

+ 5 - 0
ambari-server/src/main/python/ambari_server/setupMpacks.py

@@ -22,6 +22,7 @@ import os
 import shutil
 import json
 import ast
+import logging
 
 from ambari_commons.exceptions import FatalException
 from ambari_commons.inet_utils import download_file
@@ -35,6 +36,8 @@ from resource_management.libraries.functions.tar_archive import extract_archive,
 from resource_management.libraries.functions.version import compare_versions
 from ambari_server.setupActions import INSTALL_MPACK_ACTION, UPGRADE_MPACK_ACTION
 
+logger = logging.getLogger(__name__)
+
 MPACKS_REPLAY_LOG_FILENAME = "mpacks_replay.log"
 MPACKS_CACHE_DIRNAME = "cache"
 STACK_DEFINITIONS_RESOURCE_NAME = "stack-definitions"
@@ -657,6 +660,7 @@ def add_replay_log(mpack_command, mpack_archive_path, purge, force, verbose):
     replay_log.write("{0}\n".format(log))
 
 def install_mpack(options, replay_mode=False):
+  logger.info("Install mpack.")
   """
   Install management pack
   :param options: Command line options
@@ -674,6 +678,7 @@ def install_mpack(options, replay_mode=False):
     add_replay_log(INSTALL_MPACK_ACTION, mpack_archive_path, options.purge, options.force, options.verbose)
 
 def upgrade_mpack(options, replay_mode=False):
+  logger.info("Upgrade mpack.")
   """
   Upgrade management pack
   :param options: command line options

+ 4 - 0
ambari-server/src/main/python/ambari_server/setupSecurity.py

@@ -29,6 +29,7 @@ import shutil
 import urllib2
 import time
 import sys
+import logging
 
 from ambari_commons.exceptions import FatalException, NonFatalException
 from ambari_commons.logging_utils import print_warning_msg, print_error_msg, print_info_msg, get_verbose
@@ -54,6 +55,7 @@ from ambari_server.setupActions import SETUP_ACTION, LDAP_SETUP_ACTION
 from ambari_server.userInput import get_validated_string_input, get_prompt_default, read_password, get_YN_input, quit_if_has_answer
 from ambari_server.serverClassPath import ServerClassPath
 
+logger = logging.getLogger(__name__)
 
 REGEX_IP_ADDRESS = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
 REGEX_HOSTNAME = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"
@@ -262,6 +264,7 @@ class LdapSyncOptions:
 # Sync users and groups with configured LDAP
 #
 def sync_ldap(options):
+  logger.info("Sync users and groups with configured LDAP.")
   if not is_root():
     err = 'Ambari-server sync-ldap should be run with ' \
           'root-level privileges'
@@ -602,6 +605,7 @@ def init_ldap_properties_list_reqd(properties, options):
   return ldap_properties
 
 def setup_ldap(options):
+  logger.info("Setup LDAP.")
   if not is_root():
     err = 'Ambari-server setup-ldap should be run with ' \
           'root-level privileges'

+ 4 - 0
ambari-server/src/main/python/ambari_server/setupSso.py

@@ -17,6 +17,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 '''
+import logging
 
 from ambari_commons.os_utils import is_root, run_os_command, copy_file, set_file_permissions, remove_file
 from ambari_commons.exceptions import FatalException, NonFatalException
@@ -26,6 +27,8 @@ from ambari_server.userInput import get_validated_string_input, get_YN_input, ge
 from ambari_server.serverConfiguration import get_ambari_properties, get_value_from_properties, update_properties, \
   store_password_file
 
+logger = logging.getLogger(__name__)
+
 JWT_AUTH_ENBABLED = "authentication.jwt.enabled"
 JWT_AUTH_PROVIDER_URL = "authentication.jwt.providerUrl"
 JWT_PUBLIC_KEY = "authentication.jwt.publicKey"
@@ -46,6 +49,7 @@ JWT_PUBLIC_KEY_FOOTER = "\n-----END CERTIFICATE-----\n"
 
 
 def setup_sso(args):
+  logger.info("Setup SSO.")
   if not is_root():
     err = 'ambari-server setup-sso should be run with ' \
           'root-level privileges'

+ 15 - 2
ambari-server/src/main/python/ambari_server_main.py

@@ -20,6 +20,7 @@ limitations under the License.
 import os
 import subprocess
 import sys
+import logging
 
 from ambari_commons.exceptions import FatalException
 from ambari_commons.logging_utils import get_debug_mode, print_warning_msg, print_info_msg, \
@@ -41,6 +42,7 @@ from ambari_server.utils import check_reverse_lookup, save_pid, locate_file, loc
   save_main_pid_ex, check_exitcode
 from ambari_server.serverClassPath import ServerClassPath
 
+logger = logging.getLogger(__name__)
 
 # debug settings
 SERVER_START_DEBUG = False
@@ -215,6 +217,19 @@ def wait_for_server_start(pidFile, scmStatus):
 
 
 def server_process_main(options, scmStatus=None):
+  properties = get_ambari_properties()
+  if properties == -1:
+    err ="Error getting ambari properties"
+    raise FatalException(-1, err)
+
+  properties_for_print = []
+  logger.info("Ambari server properties config:")
+  for key, value in properties.getPropertyDict().items():
+     if "passwd" not in key and "password" not in key:
+       properties_for_print.append(key + "=" + value)
+
+  logger.info(properties_for_print)
+
   # debug mode, including stop Java process at startup
   try:
     set_debug_mode_from_options(options)
@@ -247,8 +262,6 @@ def server_process_main(options, scmStatus=None):
           "JDK manually to " + configDefaults.JDK_INSTALL_DIR
     raise FatalException(1, err)
 
-  properties = get_ambari_properties()
-
   if not options.skip_properties_validation:
     missing_properties = get_missing_properties(properties)
     if missing_properties:

+ 86 - 31
ambari-server/src/test/python/TestAmbariServer.py

@@ -38,6 +38,9 @@ import signal
 import stat
 import StringIO
 import tempfile
+import logging
+import logging.handlers
+import logging.config
 from unittest import TestCase
 os.environ["ROOT"] = ""
 
@@ -300,7 +303,10 @@ class TestAmbariServer(TestCase):
   @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
   @patch.object(_ambari_server_, "setup_security")
   @patch("optparse.OptionParser")
-  def test_main_test_setup_security(self, OptionParserMock,
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_setup_security(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, OptionParserMock,
                                     setup_security_method):
     opm = OptionParserMock.return_value
     options = MagicMock()
@@ -325,7 +331,8 @@ class TestAmbariServer(TestCase):
   @patch.object(_ambari_server_, "setup_truststore")
   @patch.object(_ambari_server_, "setup_https")
   @patch.object(_ambari_server_, "get_validated_string_input")
-  def test_setup_security(self, get_validated_string_input_mock, setup_https_mock,
+  @patch.object(_ambari_server_, "logger")
+  def test_setup_security(self, logger_mock, get_validated_string_input_mock, setup_https_mock,
                           setup_truststore_mock, setup_master_key_mock,
                           setup_ambari_krb5_jaas_mock):
 
@@ -396,7 +403,10 @@ class TestAmbariServer(TestCase):
   @patch.object(_ambari_server_, "stop")
   @patch.object(_ambari_server_, "reset")
   @patch("optparse.OptionParser")
-  def test_main_test_setup(self, OptionParserMock, reset_method, stop_method,
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_setup(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, OptionParserMock, reset_method, stop_method,
                            start_method, setup_method, exit_mock):
     opm = OptionParserMock.return_value
     options = self._create_empty_options_mock()
@@ -461,7 +471,10 @@ class TestAmbariServer(TestCase):
   @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
   @patch.object(_ambari_server_, "setup")
   @patch("optparse.OptionParser")
-  def test_main_with_preset_dbms(self, optionParserMock, setup_method):
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_with_preset_dbms(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, optionParserMock, setup_method):
     opm = optionParserMock.return_value
     options = self._create_empty_options_mock()
     args = ["setup"]
@@ -479,7 +492,11 @@ class TestAmbariServer(TestCase):
   @patch.object(_ambari_server_, "setup")
   @patch.object(_ambari_server_, "fix_database_options")
   @patch("optparse.OptionParser")
-  def test_fix_database_options_called(self, optionParserMock, fixDBOptionsMock, setup_method):
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_fix_database_options_called(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, optionParserMock,
+                                       fixDBOptionsMock, setup_method):
     opm = optionParserMock.return_value
     options = self._create_empty_options_mock()
     args = ["setup"]
@@ -498,7 +515,10 @@ class TestAmbariServer(TestCase):
   @patch.object(_ambari_server_, "stop")
   @patch.object(_ambari_server_, "reset")
   @patch("optparse.OptionParser")
-  def test_main_test_start(self, optionParserMock, reset_method, stop_method,
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_start(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, optionParserMock, reset_method, stop_method,
                            start_method, setup_method):
     opm = optionParserMock.return_value
     options = self._create_empty_options_mock()
@@ -628,7 +648,10 @@ class TestAmbariServer(TestCase):
   @patch.object(_ambari_server_, "backup")
   @patch.object(_ambari_server_, "restore")
   @patch("optparse.OptionParser")
-  def test_main_test_backup(self, optionParserMock, restore_mock, backup_mock, reset_method, stop_method,
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_backup(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, optionParserMock, restore_mock, backup_mock, reset_method, stop_method,
                            start_method, setup_method):
     opm = optionParserMock.return_value
     options = self._create_empty_options_mock()
@@ -660,7 +683,10 @@ class TestAmbariServer(TestCase):
   @patch.object(_ambari_server_, "backup")
   @patch.object(_ambari_server_, "restore")
   @patch("optparse.OptionParser")
-  def test_main_test_restore(self, optionParserMock, restore_mock, backup_mock, reset_method, stop_method,
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_restore(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, optionParserMock, restore_mock, backup_mock, reset_method, stop_method,
                             start_method, setup_method):
     opm = optionParserMock.return_value
     options = self._create_empty_options_mock()
@@ -753,7 +779,10 @@ class TestAmbariServer(TestCase):
   @patch.object(_ambari_server_, "stop")
   @patch.object(_ambari_server_, "reset")
   @patch("optparse.OptionParser")
-  def test_main_test_reset(self, optionParserMock, reset_method, stop_method,
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_reset(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, optionParserMock, reset_method, stop_method,
                            start_method, setup_method):
     opm = optionParserMock.return_value
 
@@ -3554,7 +3583,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.serverSetup.get_ambari_properties")
   @patch("ambari_commons.os_utils.search_file")
   @patch("__builtin__.open")
-  def test_setup_jce_policy(self, open_mock, search_file_mock, get_ambari_properties_mock, unpack_jce_policy_mock,
+  @patch("ambari_server.serverSetup.logger")
+  def test_setup_jce_policy(self, logger_mock, open_mock, search_file_mock, get_ambari_properties_mock, unpack_jce_policy_mock,
                             update_properties_mock, path_split_mock, shutil_copy_mock, exists_mock):
     exists_mock.return_value = True
     properties = Properties()
@@ -4234,7 +4264,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.serverSetup.get_YN_input")
   @patch("__builtin__.raw_input")
   @patch("ambari_server.serverSetup.is_root")
-  def test_reset_default(self, is_root_mock, raw_input_mock, get_YN_inputMock):
+  @patch("ambari_server.serverSetup.logger")
+  def test_reset_default(self, logger_mock, is_root_mock, raw_input_mock, get_YN_inputMock):
     is_root_mock.return_value=True
     get_YN_inputMock.return_value = False
     raw_input_mock.return_value=""
@@ -4386,7 +4417,8 @@ class TestAmbariServer(TestCase):
   @patch("getpass.getuser")
   @patch("os.chdir")
   @patch.object(ResourceFilesKeeper, "perform_housekeeping")
-  def test_start(self, perform_housekeeping_mock, chdir_mock, getuser_mock, find_jdbc_driver_mock,
+  @patch.object(_ambari_server_, "logger")
+  def test_start(self, logger_mock, perform_housekeeping_mock, chdir_mock, getuser_mock, find_jdbc_driver_mock,
                  is_root_mock, is_root_2_mock, is_root_3_mock, read_ambari_user_mock,
                  check_postgre_up_mock, print_info_msg_mock, print_warning_msg_mock,
                  find_jdk_mock, check_database_name_property_mock, search_file_mock,
@@ -4835,14 +4867,16 @@ class TestAmbariServer(TestCase):
     pass
 
   @patch.object(_ambari_server_, "BackupRestore_main")
-  def test_restore(self, bkrestore_mock):
+  @patch.object(_ambari_server_, "logger")
+  def test_restore(self, logger_mock, bkrestore_mock):
     args = ["", "/some/path/file.zip"]
     _ambari_server_.restore(args)
     self.assertTrue(bkrestore_mock.called)
     pass
 
   @patch.object(_ambari_server_, "BackupRestore_main")
-  def test_restore_no_path(self, bkrestore_mock):
+  @patch.object(_ambari_server_, "logger")
+  def test_restore_no_path(self, logger_mock, bkrestore_mock):
     args = [""]
     _ambari_server_.restore(args)
     self.assertTrue(bkrestore_mock.called)
@@ -5183,7 +5217,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.serverUpgrade.update_krb_jaas_login_properties")
   @patch("ambari_server.serverUpgrade.update_ambari_env")
   @patch("ambari_server.setupMpacks.get_replay_log_file")
-  def test_upgrade_from_161(self, get_replay_log_file_mock, update_ambari_env_mock, update_krb_jaas_login_properties_mock, move_user_custom_actions_mock, upgrade_local_repo_mock, get_ambari_properties_mock,
+  @patch("ambari_server.serverUpgrade.logger")
+  def test_upgrade_from_161(self, logger_mock, get_replay_log_file_mock, update_ambari_env_mock, update_krb_jaas_login_properties_mock, move_user_custom_actions_mock, upgrade_local_repo_mock, get_ambari_properties_mock,
                             get_ambari_properties_2_mock, get_ambari_properties_3_mock, get_ambari_version_mock, write_property_mock,
                             is_root_mock, update_ambari_properties_mock, find_properties_file_mock, run_os_command_mock,
                             run_schema_upgrade_mock, read_ambari_user_mock, print_warning_msg_mock,
@@ -6189,7 +6224,8 @@ class TestAmbariServer(TestCase):
   @patch('__builtin__.raw_input')
   @patch("ambari_server.serverSetup.disable_security_enhancements")
   @patch("ambari_server.serverSetup.expand_jce_zip_file")
-  def test_setup_remote_db_wo_client(self, expand_jce_zip_file_mock, check_selinux_mock, raw_input, configure_os_settings_mock,
+  @patch("ambari_server.serverSetup.logger")
+  def test_setup_remote_db_wo_client(self, logger_mock, expand_jce_zip_file_mock, check_selinux_mock, raw_input, configure_os_settings_mock,
                                      download_jdk_mock, check_ambari_user_mock, is_root_mock, check_jdbc_drivers_mock,
                                      read_password_mock, ensure_jdbc_driver_installed_mock, store_remote_properties_mock,
                                      get_validated_string_input_0_mock, get_YN_input_0_mock,
@@ -7139,7 +7175,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.search_file")
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_setup_ldap_invalid_input(self, is_root_method, get_ambari_properties_method,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_setup_ldap_invalid_input(self, logger_mock, is_root_method, get_ambari_properties_method,
                                     search_file_message,
                                     update_properties_method,
                                     get_YN_input_method,
@@ -7245,7 +7282,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.is_root")
   @patch("ambari_server.setupSecurity.read_password")
   @patch("os.path.exists")
-  def test_setup_ldap(self, exists_method, read_password_method, is_root_method, get_ambari_properties_method,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_setup_ldap(self, logger_mock, exists_method, read_password_method, is_root_method, get_ambari_properties_method,
                       search_file_message,
                       get_validated_string_input_method,
                       configure_ldap_password_method, update_properties_method,
@@ -7381,7 +7419,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_ldap_sync_all(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_ldap_sync_all(self, logger_mock, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
       get_validated_string_input_mock, urlopen_mock):
 
     is_root_method.return_value = True
@@ -7423,7 +7462,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_ldap_sync_users(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_ldap_sync_users(self, logger_mock, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
                          get_validated_string_input_mock, urlopen_mock, os_path_exists_mock, open_mock):
 
     os_path_exists_mock.return_value = 1
@@ -7469,7 +7509,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_ldap_sync_groups(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_ldap_sync_groups(self, logger_mock, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
                            get_validated_string_input_mock, urlopen_mock, os_path_exists_mock, open_mock):
 
     os_path_exists_mock.return_value = 1
@@ -7513,7 +7554,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_ldap_sync_ssl(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_ldap_sync_ssl(self, logger_mock, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
                          get_validated_string_input_mock, urlopen_mock):
 
     is_root_method.return_value = True
@@ -7556,7 +7598,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_ldap_sync_existing(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_ldap_sync_existing(self, logger_mock, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
                          get_validated_string_input_mock, urlopen_mock):
 
     is_root_method.return_value = True
@@ -7591,7 +7634,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_ldap_sync_no_sync_mode(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_ldap_sync_no_sync_mode(self, logger_mock, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
                      get_validated_string_input_mock, urlopen_mock):
 
     is_root_method.return_value = True
@@ -7627,7 +7671,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.is_root")
-  def test_ldap_sync_error_status(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_ldap_sync_error_status(self, logger_mock, is_root_method, is_server_runing_mock, get_ambari_properties_mock,
       get_validated_string_input_mock, urlopen_mock):
 
     is_root_method.return_value = True
@@ -7664,7 +7709,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.get_ambari_properties")
   @patch("ambari_server.setupSecurity.get_validated_string_input")
-  def test_sync_ldap_forbidden(self, get_validated_string_input_method, get_ambari_properties_method,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_sync_ldap_forbidden(self, logger_mock, get_validated_string_input_method, get_ambari_properties_method,
                                 is_server_runing_method, is_root_method,
                                 encodestring_method, request_constructor, urlopen_method):
 
@@ -7745,7 +7791,8 @@ class TestAmbariServer(TestCase):
 
   @patch("ambari_server.setupSecurity.is_root")
   @patch("ambari_server.setupSecurity.is_server_runing")
-  def test_sync_ldap_ambari_stopped(self, is_server_runing_method, is_root_method):
+  @patch("ambari_server.setupSecurity.logger")
+  def test_sync_ldap_ambari_stopped(self, logger_mock, is_server_runing_method, is_root_method):
     is_root_method.return_value = True
     is_server_runing_method.return_value = (None, None)
 
@@ -7767,7 +7814,8 @@ class TestAmbariServer(TestCase):
   @patch("ambari_server.setupSecurity.is_root")
   @patch("ambari_server.setupSecurity.is_server_runing")
   @patch("ambari_server.setupSecurity.get_ambari_properties")
-  def test_sync_ldap_not_configured(self, get_ambari_properties_method,
+  @patch("ambari_server.setupSecurity.logger")
+  def test_sync_ldap_not_configured(self, logger_mock,  get_ambari_properties_method,
                      is_server_runing_method, is_root_method):
     is_root_method.return_value = True
     is_server_runing_method.return_value = (True, None)
@@ -8430,7 +8478,8 @@ class TestAmbariServer(TestCase):
   @patch("os.path.isfile")
   @patch("sys.exit")
   @patch("ambari_server.userInput.get_YN_input")
-  def test_update_host_names(self, getYNInput_mock, sysExitMock, isFileMock, getJavaExePathMock,
+  @patch("ambari_server.hostUpdate.logger")
+  def test_update_host_names(self, logger_mock, getYNInput_mock, sysExitMock, isFileMock, getJavaExePathMock,
                              getAmbariPropertiesMock, parsePropertiesFileMock, ensureDriverInstalledMock, readAmbariUserMock,
                              ensureCanStartUnderCurrentUserMock, generateEnvMock, runOSCommandMock, isServerRunningMock):
     properties = Properties()
@@ -8465,7 +8514,10 @@ class TestAmbariServer(TestCase):
   @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
   @patch.object(_ambari_server_, "is_server_runing")
   @patch("optparse.OptionParser")
-  def test_main_test_status_running(self, optionParserMock, is_server_runing_method):
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_status_running(self, setup_logging_mock, get_ambari_properties_mock, logger_mock,  optionParserMock, is_server_runing_method):
     opm = optionParserMock.return_value
     options = self._create_empty_options_mock()
     del options.exit_message
@@ -8491,7 +8543,10 @@ class TestAmbariServer(TestCase):
   @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
   @patch.object(_ambari_server_, "is_server_runing")
   @patch("optparse.OptionParser")
-  def test_main_test_status_not_running(self, optionParserMock, is_server_runing_method):
+  @patch.object(_ambari_server_, "logger")
+  @patch("ambari_server.serverConfiguration.get_ambari_properties")
+  @patch.object(_ambari_server_, "setup_logging")
+  def test_main_test_status_not_running(self, setup_logging_mock, get_ambari_properties_mock, logger_mock, optionParserMock, is_server_runing_method):
     opm = optionParserMock.return_value
     options = self._create_empty_options_mock()
     del options.exit_message

部分文件因文件數量過多而無法顯示