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 30, 2016
1 parent e797cbd commit b5b3523
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
20 changes: 20 additions & 0 deletions storops/__init__.py
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:
log.addHandler(logging.StreamHandler(sys.stdout))


def disable_log():
log = logging.getLogger(__name__)
log.setLevel(logging.NOTSET)
log.handlers = []
24 changes: 23 additions & 1 deletion test/__init__.py
Expand Up @@ -18,7 +18,7 @@
import logging
import unittest

from hamcrest import assert_that, not_none
from hamcrest import assert_that, not_none, equal_to, instance_of

from test.unity.rest_mock import patch_rest
from test.vnx.cli_mock import patch_cli
Expand Down Expand Up @@ -50,3 +50,25 @@ def test_vnx_enum_availability(self):
def test_unity_enum_availability(self):
raid5 = storops.RaidTypeEnum.RAID5
assert_that(raid5, not_none())

def test_enable_log(self):
storops.enable_log()
log = logging.getLogger('storops')
assert_that(len(log.handlers), equal_to(1))
assert_that(log.handlers[0], instance_of(logging.StreamHandler))
assert_that(log.getEffectiveLevel(), equal_to(logging.DEBUG))

def test_enable_log_called_twice(self):
storops.enable_log()
storops.enable_log(logging.INFO)
log = logging.getLogger('storops')
assert_that(len(log.handlers), equal_to(1))
assert_that(log.handlers[0], instance_of(logging.StreamHandler))
assert_that(log.getEffectiveLevel(), equal_to(logging.INFO))

def test_disable_log(self):
storops.enable_log()
storops.disable_log()
log = logging.getLogger('storops')
assert_that(len(log.handlers), equal_to(0))
assert_that(log.getEffectiveLevel(), equal_to(logging.NOTSET))

0 comments on commit b5b3523

Please sign in to comment.