Repository: dbt-labs/dbt-core

Issue:
dbt reporting FileExistsError [WinError 183]
### Describe the bug
When running any dbt commands when UTC timezone is 1 calendar day ahead of the local timezone dbt can't create a new log file. The error returned is:
```FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'logs\\dbt.log' -> ```
This occurred running dbt 15b2 on a windows based OS.

### Steps To Reproduce
Check to see if your local time is 1 calendar date behind UTC. A use-case maybe running dbt at or near midnight local-time (UTC-5 in my case) and attempt to compile a dbt project.

### Expected behavior
dbt will return an FileExistError: [WinError 183].
### Screenshots and log output
```
2019-10-25 03:10:31,150080 (MainThread): Running with dbt=0.15.0-b2
2019-10-25 03:10:31,478080 (MainThread): running dbt with arguments Namespace(cls=<class 'dbt.task.test.TestTask'>, data=False, debug=True, exclude=None, log_cache_events=False, log_format='default', models=['dim_agency_assert_agency_id'], partial_parse=None, profile=None, profiles_dir='C:\\Users\\dbt_fan\\.dbt', project_dir=None, record_timing_info=None, rpc_method='test', schema=False, single_threaded=False, strict=False, target=None, test_new_parser=False, threads=None, use_cache=True, vars='{}', version_check=True, warn_error=False, which='test', write_json=True)
2019-10-25 03:10:31,480087 (MainThread): Encountered an error:
2019-10-25 03:10:31,480087 (MainThread): [WinError 183] Cannot create a file when that file already exists: 'logs\\dbt.log' -> 'C:\\source\\my_dbt\\logs\\dbt-2019-10-24.log'
2019-10-25 03:10:31,482083 (MainThread): Traceback (most recent call last):
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\dbt\main.py", line 80, in main
    results, succeeded = handle_and_check(args)
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\dbt\main.py", line 158, in handle_and_check
    task, res = run_from_args(parsed)
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\dbt\main.py", line 204, in run_from_args
    log_manager.set_path(log_path)
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\dbt\logger.py", line 483, in set_path
    self._file_handler.set_path(path)
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\dbt\logger.py", line 383, in set_path
    self._replay_buffered()
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\dbt\logger.py", line 402, in _replay_buffered
    super().emit(record)
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\logbook\handlers.py", line 980, in emit
    self.perform_rollover(new_timestamp)
  File "c:\users\dbt_fan\appdata\local\programs\python\python37\lib\site-packages\logbook\handlers.py", line 962, in perform_rollover
    os.rename(self._filename, filename)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'logs\\dbt.log' -> 'C:\\source\\my_dbt\\logs\\dbt-2019-10-24.log'
```
### System information
**Which database are you using dbt with?**
- [ ] postgres
- [ ] redshift
- [ ] bigquery
- [* ] snowflake
- [ ] other (specify: ____________)


**The output of `dbt --version`:**
```
installed version: 0.15.0-b2
latest version: 0.14.3
```

**The operating system you're using:**
Windows 10
**The output of `python --version`:**
Python 3.7.2
### Additional context
Add any other context about the problem here.



Discussion:
This looks like this bug: https://github.com/getlogbook/logbook/issues/280

There's a lot of weird stuff here though, and I think it's because logbook's `TimedRotatingFileHandler` depends upon the file's `mtime`. I'm inclined to fix this by changing to a size-based rotating handler instead of time.

Resolving patch:
diff --git a/core/dbt/logger.py b/core/dbt/logger.py
--- a/core/dbt/logger.py
+++ b/core/dbt/logger.py
@@ -334,13 +334,15 @@ def make_record(self, message, exception, filename, lineno):
 _redirect_std_logging()
 
 
-class DelayedFileHandler(logbook.TimedRotatingFileHandler, FormatterMixin):
+class DelayedFileHandler(logbook.RotatingFileHandler, FormatterMixin):
     def __init__(
         self,
         log_dir: Optional[str] = None,
         level=logbook.DEBUG,
         filter=None,
         bubble=True,
+        max_size=10 * 1024 * 1024,  # 10 mb
+        backup_count=5,
     ) -> None:
         self.disabled = False
         self._msg_buffer: Optional[List[logbook.LogRecord]] = []
@@ -352,6 +354,8 @@ def __init__(
         if log_dir is not None:
             self.set_path(log_dir)
         self._text_format_string = None
+        self._max_size = max_size
+        self._backup_count = backup_count
 
     def reset(self):
         if self.initialized:
@@ -384,16 +388,16 @@ def set_path(self, log_dir):
         self._log_path = log_path
 
     def _super_init(self, log_path):
-        logbook.TimedRotatingFileHandler.__init__(
+        logbook.RotatingFileHandler.__init__(
             self,
             filename=log_path,
             level=self.level,
             filter=self.filter,
+            delay=True,
+            max_size=self._max_size,
+            backup_count=self._backup_count,
             bubble=self.bubble,
             format_string=DEBUG_LOG_FORMAT,
-            date_format='%Y-%m-%d',
-            backup_count=7,
-            timed_filename_for_current=False,
         )
         FormatterMixin.__init__(self, DEBUG_LOG_FORMAT)
 


Test patch: