Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/logger setup #15

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9f5861c
AO - adding logger capabilities along with Documentation for installa…
joyoyoyoyoyo Jan 10, 2017
7497e16
AO - deleted logger class to now use Logbook logger
joyoyoyoyoyo Jan 17, 2017
7e8fa2e
AO - added a README for logger setup, along with Installation process
joyoyoyoyoyo Jan 17, 2017
c2848b2
AO - Added Unit-test for Logger that outputs to the system's standard…
joyoyoyoyoyo Jan 17, 2017
87f70b1
AO - temporarily make a new file for log levels. I'm sleepy
joyoyoyoyoyo Jan 17, 2017
167ebd1
AO - adjusted logger to a shorter name for easier conventions, and I …
joyoyoyoyoyo Jan 17, 2017
1f5c31f
AO - stdout stream is now being closed in the unit test tearDown() fu…
joyoyoyoyoyo Jan 17, 2017
eda3640
AO - Logging unit test is successful w.r.t. standard-output logging
joyoyoyoyoyo Jan 17, 2017
b78e211
AO - created log to file system using Python module tempfile--any arb…
joyoyoyoyoyo Jan 18, 2017
b539957
AO - hotfix: created log to file system using Python module tempfile-…
joyoyoyoyoyo Jan 18, 2017
cd4c8b0
Update .gitignore to ignore log files (.log)
Jan 19, 2017
caf4f71
Merge branch 'master' into feature/logger-setup
Jan 19, 2017
c8b80d7
AO - adding logger capabilities along with Documentation for installa…
joyoyoyoyoyo Jan 10, 2017
6e2a68b
AO - deleted logger class to now use Logbook logger
joyoyoyoyoyo Jan 17, 2017
58a8d4f
AO - added a README for logger setup, along with Installation process
joyoyoyoyoyo Jan 17, 2017
6d7cd60
AO - Added Unit-test for Logger that outputs to the system's standard…
joyoyoyoyoyo Jan 17, 2017
302f234
AO - temporarily make a new file for log levels. I'm sleepy
joyoyoyoyoyo Jan 17, 2017
23fcb11
AO - adjusted logger to a shorter name for easier conventions, and I …
joyoyoyoyoyo Jan 17, 2017
88ac882
AO - stdout stream is now being closed in the unit test tearDown() fu…
joyoyoyoyoyo Jan 17, 2017
972b561
AO - Logging unit test is successful w.r.t. standard-output logging
joyoyoyoyoyo Jan 17, 2017
8e877c7
AO - created log to file system using Python module tempfile--any arb…
joyoyoyoyoyo Jan 18, 2017
01123fc
AO - hotfix: created log to file system using Python module tempfile-…
joyoyoyoyoyo Jan 18, 2017
f858735
Merge branch 'feature/logger-setup' of github.com:UCSB-CS189-2016-17-…
joyoyoyoyoyo Feb 12, 2017
29bff6f
Merge branch 'master' of github.com:UCSB-CS189-2016-17-Aerospace/Aero…
joyoyoyoyoyo Feb 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@
*.pyc
*__pycache__


# Log files
*.log

# IDE-related files
.idea/*
.idea/*
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions logger/log-levels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# make sure to sync these up with _speedups.pyx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is _speedups.pyx?

CRITICAL = 15
ERROR = 14
WARNING = 13
NOTICE = 12
INFO = 11
DEBUG = 10
TRACE = 9
NOTSET = 0

_level_names = {
CRITICAL: 'CRITICAL',
ERROR: 'ERROR',
WARNING: 'WARNING',
NOTICE: 'NOTICE',
INFO: 'INFO',
DEBUG: 'DEBUG',
TRACE: 'TRACE',
NOTSET: 'NOTSET'
}
16 changes: 16 additions & 0 deletions logger/log-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Installation of Logbook
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would list this as a requirement in requirements.txt found in the root-level directory.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would rename this to README.md and make it a broader document. Logbook Setup could be a section, but we could also include other sections for usage, to-do's, etc.

```
cd Aerocube
pip install logbook
# pip3 install logbook
cd logger/tests
# python3 TestLogger.py
# python3.5 TestLogger.py
```


# Todo's for Log book
1. Initialize a handler for streaming logs to sys.stdout
2. Initialize a handler for streaming logs to arbitrary files

```
127 changes: 127 additions & 0 deletions logger/tests/TestFileLogHandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import unittest
from tempfile import NamedTemporaryFile
from logbook import FileHandler, Logger, TestHandler


class TestFileLogHandler(unittest.TestCase):

def setUp(self):
"""
Initialize Handler for arbitrary streams
with a parameter to log to the standard output.
Logger can be any name. The logger itself is a log channel
or a record dispatcher. In this case, the logger is set to
channel logs to the standard output
:return:
"""
# Create an arbitrary temporary file
self._tempfile = NamedTemporaryFile(delete=True)

# Setup logging handler for standard output purposes
self._output_stream = FileHandler(
filename=self._tempfile.name,
encoding='UTF-8',
bubble=True
)
self._ctx = self._output_stream.push_application()

# Setup logging handler for testing purposes
self._log_handler = TestHandler(bubble=True)
self._log_handler.push_thread()

# Initialize Logger
# Apply both standard-output and test log-handlers to the newly, initialized logger
self._log = Logger('TestLog')
self._log.handlers = [self._log_handler, self._output_stream]

# string helper
self._tempfile_buffer = ''

def tearDown(self):
"""
Close the output stream.
:return:
"""
self._output_stream.close()
self._log_handler.pop_thread()
self._log_handler.close()
self._tempfile.close()


def test_logger_init(self):
"""
Call all log methods
:return:
"""
self.assertIsNotNone(self._log, msg='Logger resource not found')

def test_warn_logging(self):
self._log.warn('warn000.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('warn000.log' in self._tempfile_buffer)

self._log.warn('warn001.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('warn001.log' in self._tempfile_buffer)
self.assertFalse('warn002.log' in self._tempfile_buffer)


def test_error_logging(self):
self._log.error('error000.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('error000.log' in self._tempfile_buffer)

self._log.error('error001.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('error001.log' in self._tempfile_buffer)
self.assertFalse('error002.log' in self._tempfile_buffer)

def test_critical_logging(self):
self._log.critical('critical000.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('critical000.log' in self._tempfile_buffer)

self._log.critical('critical001.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('critical001.log' in self._tempfile_buffer)
self.assertFalse('critical002.log' in self._tempfile_buffer)

def test_debug_logging(self):
self._log.debug('debug000.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('debug000.log' in self._tempfile_buffer)

self._log.debug('debug001.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('debug001.log' in self._tempfile_buffer)
self.assertFalse('debug002.log' in self._tempfile_buffer)

def test_info_logging(self):
self._log.info('info000.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('info000.log' in self._tempfile_buffer)

self._log.info('info001.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('info001.log' in self._tempfile_buffer)
self.assertFalse('info002.log' in self._tempfile_buffer)

def test_notice_logging(self):
self._log.notice('notice000.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('notice000.log' in self._tempfile_buffer)

self._log.notice('notice001.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('notice001.log' in self._tempfile_buffer)
self.assertFalse('notice002.log' in self._tempfile_buffer)

def test_trace_logging(self):
self._log.trace('trace000.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('trace000.log' in self._tempfile_buffer)

self._log.trace('trace001.log')
self._tempfile_buffer = str(self._tempfile.read())
self.assertTrue('trace001.log' in self._tempfile_buffer)
self.assertFalse('trace002.log' in self._tempfile_buffer)
112 changes: 112 additions & 0 deletions logger/tests/TestLogbook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import unittest
import sys
from logbook import StreamHandler, Logger, TestHandler


class TestLogbook(unittest.TestCase):

def setUp(self):
"""
Initialize Handler for arbitrary streams
with a parameter to log to the standard output.
Logger can be any name. The logger itself is a log channel
or a record dispatcher. In this case, the logger is set to
channel logs to the standard output
:return:
"""
# Setup logging handler for standard output purposes
self._output_stream = StreamHandler(sys.stdout)
self._ctx = self._output_stream.push_application()

# Setup logging handler for testing purposes
self._log_handler = TestHandler(bubble=True)
self._log_handler.push_thread()

# Initialize Logger
# Apply both standard-output and test log-handlers to the newly, initialized logger
self._log = Logger('TestLog')
self._log.handlers = [self._log_handler, self._output_stream]

def tearDown(self):
"""
Close the output stream.
:return:
"""
self._output_stream.close()
self._log_handler.pop_thread()
self._log_handler.close()

def test_logger_init(self):
"""
Call all log methods
:return:
"""
self.assertIsNotNone(self._log, msg='Logger resource not found')

def test_warn_logging(self):
self.assertFalse(self._log_handler.has_warning('warn000.log'))
self._log.warn('warn000.log')
self.assertTrue(self._log_handler.has_warning)
self._log.warn('warn001.log')
self.assertTrue(self._log_handler.has_warning('warn000.log'))
self.assertTrue(self._log_handler.has_warning('warn001.log'))
self.assertFalse(self._log_handler.has_warning('warn002.log'))

def test_error_logging(self):
self.assertFalse(self._log_handler.has_errors)
self.assertFalse(self._log_handler.has_error('error000.log'))
self._log.error('error000.log')
self.assertTrue(self._log_handler.has_error)
self._log.error('error001.log')
self.assertTrue(self._log_handler.has_error('error000.log'))
self.assertTrue(self._log_handler.has_error('error001.log'))
self.assertFalse(self._log_handler.has_error('error002.log'))
self.assertTrue(self._log_handler.has_errors)

def test_critical_logging(self):
self.assertFalse(self._log_handler.has_criticals)
self.assertFalse(self._log_handler.has_critical('critical000.log'))
self._log.critical('critical000.log')
self.assertTrue(self._log_handler.has_error)
self._log.critical('critical001.log')
self.assertTrue(self._log_handler.has_critical('critical000.log'))
self.assertTrue(self._log_handler.has_critical('critical001.log'))
self.assertFalse(self._log_handler.has_critical('critical002.log'))
self.assertTrue(self._log_handler.has_criticals)

def test_debug_logging(self):
self.assertFalse(self._log_handler.has_debugs)
self.assertFalse(self._log_handler.has_debug('debug000.log'))
self._log.debug('debug000.log')
self.assertTrue(self._log_handler.has_debug)
self._log.debug('debug001.log')
self.assertTrue(self._log_handler.has_debug('debug000.log'))
self.assertTrue(self._log_handler.has_debug('debug001.log'))
self.assertFalse(self._log_handler.has_debug('debug002.log'))
self.assertTrue(self._log_handler.has_debugs)

def test_info_logging(self):
self.assertFalse(self._log_handler.has_infos)
self.assertFalse(self._log_handler.has_info('info000.log'))
self._log.info('info000.log')
self.assertTrue(self._log_handler.has_info)
self._log.info('info001.log')
self.assertTrue(self._log_handler.has_info('info000.log'))
self.assertTrue(self._log_handler.has_info('info001.log'))
self.assertFalse(self._log_handler.has_info('info002.log'))
self.assertTrue(self._log_handler.has_infos)

def test_notice_logging(self):
self.assertFalse(self._log_handler.has_notices)
self.assertFalse(self._log_handler.has_notice('notice000.log'))
self._log.notice('notice000.log')
self.assertTrue(self._log_handler.has_notice)
self._log.notice('notice001.log')
self.assertTrue(self._log_handler.has_notice('notice000.log'))
self.assertTrue(self._log_handler.has_notice('notice001.log'))
self.assertFalse(self._log_handler.has_notice('notice002.log'))
self.assertTrue(self._log_handler.has_notices)

@unittest.skip('Trace test is skipped, because the log handler has no `has_trace` field\n')
def test_trace_logging(self):
self._log.trace('Trace')
24 changes: 24 additions & 0 deletions logger/tests/TestLogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from logbook import Logger
import unittest
#from ../Logger import Logger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this line just to keep things clean, unless it's kept in here for some reason.


class TestLogger(unittest.TestCase):

def setUp(self):
# Declare the Logger
self._log_handler = logbook.TestHandler()
self._log_handler.push_thread()
self._logger = Logger('TestLogger')

def tearDown(self):
self._log_handler.pop_thread()
self._logger = None

#@classmethod
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also remove these commented lines if they aren't serving a purpose.

#def instantiateState(logger):


def testLoggerWithWarning():
with logbook.TestHandler() as handler:
_logger.warn('A warning')
assert logger.has_warning('A warning')