Skip to content

Commit

Permalink
[GH 59] Enable/disable console logging
Browse files Browse the repository at this point in the history
Add an API on storops module to enabled and disable the console logging
  • Loading branch information
Tianqi-Tang committed Nov 25, 2016
1 parent e797cbd commit 66291ba
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions storops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@
# under the License.
from __future__ import unicode_literals

import sys
import logging

from storops.vnx.resource.system import VNXSystem # noqa
from storops.unity.resource.system import UnitySystem # noqa
from storops.vnx.enums import * # noqa
from storops.unity.enums import * # noqa

__author__ = 'Cedric Zhuang'


def enable_log(level=logging.DEBUG):
"""Enable console logging.
This is a utils method for try run with storops.
"""
log = logging.getLogger(__name__)
log.setLevel(level)
if not log.handlers:
_handler = logging.StreamHandler(sys.stdout)
log.addHandler(_handler)


def disable_log():
log = logging.getLogger(__name__)
log.handlers = []
44 changes: 44 additions & 0 deletions test/test_storops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# coding=utf-8
# Copyright (c) 2015 EMC Corporation.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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.
from __future__ import unicode_literals

import logging
from hamcrest import assert_that, equal_to

import storops


def test_enabled_logger():
storops.enable_log()
storops.enable_log()
log = logging.getLogger('storops')
assert_that(len(log.handlers), equal_to(1))
assert_that(log.getEffectiveLevel(), equal_to(logging.DEBUG))


def test_enable_logger_called_twice():
storops.enable_log()
storops.enable_log(logging.INFO)
log = logging.getLogger('storops')
assert_that(len(log.handlers), equal_to(1))
assert_that(log.getEffectiveLevel(), equal_to(logging.INFO))


def test_disable_logger():
storops.enable_log()
storops.disable_log()
log = logging.getLogger('storops')
assert_that(len(log.handlers), equal_to(0))

0 comments on commit 66291ba

Please sign in to comment.