Repository: pymedusa/Medusa

Issue:
codec can't encode characters in position 29-36
### Before submitting your issue:

Enable debug logging in Medusa settings, reproduce the error (be sure to disable after the bug is fixed)

**Branch/Commit:** feature/add-indexerids-to-db/4bdbd81
**OS:** windows
**What you did:** Started up medusa while having the series `Tokyo Goul` added. With scene exceptions added from xem.
**What happened:** The error below showed.
**What you expected:** no error.
**Logs:**
```
2017-12-27 21:29:34 ERROR    MAIN :: [4bdbd81] BraceMessage string formatting failed. Using representation instead.
  File "D:\JetBrains\PyCharm 2017.2.4\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\JetBrains\PyCharm 2017.2.4\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:/Development/Medusa5/start.py", line 7, in <module>
    main()
  File "D:/Development/Medusa5\medusa\__main__.py", line 2109, in main
    application.start(sys.argv[1:])
  File "D:/Development/Medusa5\medusa\__main__.py", line 354, in start
    name_cache.build_name_cache()
  File "D:/Development/Medusa5\medusa\name_cache.py", line 128, in build_name_cache
    _cache_name(show)
  File "D:/Development/Medusa5\medusa\name_cache.py", line 116, in _cache_name
    'names': ', '.join(names.keys())
  File "D:/Development/Medusa5\medusa\logger\adapters\style.py", line 89, in log
    self.logger.log(level, brace_msg, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1489, in log
    self.logger.log(level, msg, *args, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1231, in log
    self._log(level, msg, args, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1286, in _log
    self.handle(record)
  File "D:\Python27\lib\logging\__init__.py", line 1296, in handle
    self.callHandlers(record)
  File "D:\Python27\lib\logging\__init__.py", line 1336, in callHandlers
    hdlr.handle(record)
  File "D:\Python27\lib\logging\__init__.py", line 759, in handle
    self.emit(record)
  File "D:\Python27\lib\logging\handlers.py", line 78, in emit
    logging.FileHandler.emit(self, record)
  File "D:\Python27\lib\logging\__init__.py", line 957, in emit
    StreamHandler.emit(self, record)
  File "D:\Python27\lib\logging\__init__.py", line 861, in emit
    msg = self.format(record)
  File "D:\Python27\lib\logging\__init__.py", line 734, in format
    return fmt.format(record)
  File "D:/Development/Medusa5\medusa\logger\__init__.py", line 546, in format
    msg = super(CensoredFormatter, self).format(record)
  File "D:\Python27\lib\logging\__init__.py", line 465, in format
    record.message = record.getMessage()
  File "D:\Python27\lib\logging\__init__.py", line 325, in getMessage
    msg = str(self.msg)
  File "D:/Development/Medusa5\medusa\init\logconfig.py", line 80, in __str__
    result = text_type(self.fmt)
  File "D:/Development/Medusa5\medusa\logger\adapters\style.py", line 49, in __str__
    ''.join(traceback.format_stack()),
Traceback (most recent call last):
  File "D:/Development/Medusa5\medusa\logger\adapters\style.py", line 39, in __str__
    return msg.format(*args, **kwargs)
  File "D:\Python27\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 29-36: character maps to <undefined>
```

I got it in my branch, but I doubt it was because of any of the changed I did.


Discussion:
@p0psicles can you include the log lines prior to this and include at least the actual representation of the BraceMessage
```
2017-12-28 12:16:23 DEBUG    MAIN :: [15fecef] Internal name cache for Pene e Vagina set to: pene e vagina
2017-12-28 12:16:23 ERROR    MAIN :: [15fecef] BraceMessage string formatting failed. Using representation instead.
  File "D:\JetBrains\PyCharm 2017.2.4\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\JetBrains\PyCharm 2017.2.4\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:/Development/Medusa/start.py", line 7, in <module>
    main()
  File "D:/Development/Medusa\medusa\__main__.py", line 2108, in main
    application.start(sys.argv[1:])
  File "D:/Development/Medusa\medusa\__main__.py", line 353, in start
    name_cache.build_name_cache()
  File "D:/Development/Medusa\medusa\name_cache.py", line 119, in build_name_cache
    _cache_name(show)
  File "D:/Development/Medusa\medusa\name_cache.py", line 107, in _cache_name
    'names': ', '.join(names.keys())
  File "D:/Development/Medusa\medusa\logger\adapters\style.py", line 89, in log
    self.logger.log(level, brace_msg, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1489, in log
    self.logger.log(level, msg, *args, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1231, in log
    self._log(level, msg, args, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1286, in _log
    self.handle(record)
  File "D:\Python27\lib\logging\__init__.py", line 1296, in handle
    self.callHandlers(record)
  File "D:\Python27\lib\logging\__init__.py", line 1336, in callHandlers
    hdlr.handle(record)
  File "D:\Python27\lib\logging\__init__.py", line 759, in handle
    self.emit(record)
  File "D:\Python27\lib\logging\__init__.py", line 861, in emit
    msg = self.format(record)
  File "D:\Python27\lib\logging\__init__.py", line 734, in format
    return fmt.format(record)
  File "D:/Development/Medusa\medusa\logger\__init__.py", line 546, in format
    msg = super(CensoredFormatter, self).format(record)
  File "D:\Python27\lib\logging\__init__.py", line 465, in format
    record.message = record.getMessage()
  File "D:\Python27\lib\logging\__init__.py", line 325, in getMessage
    msg = str(self.msg)
  File "D:/Development/Medusa\medusa\init\logconfig.py", line 80, in __str__
    result = text_type(self.fmt)
  File "D:/Development/Medusa\medusa\logger\adapters\style.py", line 49, in __str__
    ''.join(traceback.format_stack()),
Traceback (most recent call last):
  File "D:/Development/Medusa\medusa\logger\adapters\style.py", line 39, in __str__
    return msg.format(*args, **kwargs)
  File "D:\Python27\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 26-28: character maps to <undefined>
2017-12-28 12:16:23 DEBUG    MAIN :: [15fecef] BraceMessage(u'Internal name cache for {show} set to: {names}', {'names': u'naruto shippuuden, naruto \u75be\u98a8\u4f1d, naruto shippuden', 'show': u'Naruto Shippuden'}, extra=None)
2017-12-28 12:16:23 ERROR    MAIN :: [15fecef] BraceMessage string formatting failed. Using representation instead.
  File "D:\JetBrains\PyCharm 2017.2.4\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\JetBrains\PyCharm 2017.2.4\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:/Development/Medusa/start.py", line 7, in <module>
    main()
  File "D:/Development/Medusa\medusa\__main__.py", line 2108, in main
    application.start(sys.argv[1:])
  File "D:/Development/Medusa\medusa\__main__.py", line 353, in start
    name_cache.build_name_cache()
  File "D:/Development/Medusa\medusa\name_cache.py", line 119, in build_name_cache
    _cache_name(show)
  File "D:/Development/Medusa\medusa\name_cache.py", line 107, in _cache_name
    'names': ', '.join(names.keys())
  File "D:/Development/Medusa\medusa\logger\adapters\style.py", line 89, in log
    self.logger.log(level, brace_msg, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1489, in log
    self.logger.log(level, msg, *args, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1231, in log
    self._log(level, msg, args, **kwargs)
  File "D:\Python27\lib\logging\__init__.py", line 1286, in _log
    self.handle(record)
  File "D:\Python27\lib\logging\__init__.py", line 1296, in handle
    self.callHandlers(record)
  File "D:\Python27\lib\logging\__init__.py", line 1336, in callHandlers
    hdlr.handle(record)
  File "D:\Python27\lib\logging\__init__.py", line 759, in handle
    self.emit(record)
  File "D:\Python27\lib\logging\handlers.py", line 76, in emit
    if self.shouldRollover(record):
  File "D:\Python27\lib\logging\handlers.py", line 156, in shouldRollover
    msg = "%s\n" % self.format(record)
  File "D:\Python27\lib\logging\__init__.py", line 734, in format
    return fmt.format(record)
  File "D:/Development/Medusa\medusa\logger\__init__.py", line 546, in format
    msg = super(CensoredFormatter, self).format(record)
  File "D:\Python27\lib\logging\__init__.py", line 465, in format
    record.message = record.getMessage()
  File "D:\Python27\lib\logging\__init__.py", line 325, in getMessage
    msg = str(self.msg)
  File "D:/Development/Medusa\medusa\init\logconfig.py", line 80, in __str__
    result = text_type(self.fmt)
  File "D:/Development/Medusa\medusa\logger\adapters\style.py", line 49, in __str__
    ''.join(traceback.format_stack()),
Traceback (most recent call last):
  File "D:/Development/Medusa\medusa\logger\adapters\style.py", line 39, in __str__
    return msg.format(*args, **kwargs)
  File "D:\Python27\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
```
I still think the issue is the recently added `str()` in the brace message
using text_type() in stead is no solution. As on python2.7 that just translates to str().
medusa/logger/adapters/style.py:L39
names (unicode): `u'fairy tail 2, fairy tail s2, フェアリーテイル, fairy tail, fairy tail 2014'`
series (unicode): `fairy tail`
msg (str): `'Internal name cache for {series} set to: {names}'`
I **think** it is caused when logging a dict with format.
It's because the msg is str and the names/series unicode. It can't mix them. It's fixed if you decode the msg to unicode like this: `msg.decode('utf-8').format(*args, **kwargs)`
Shall I push the fix?

Resolving patch:
diff --git a/medusa/logger/adapters/style.py b/medusa/logger/adapters/style.py
--- a/medusa/logger/adapters/style.py
+++ b/medusa/logger/adapters/style.py
@@ -33,7 +33,7 @@ def __str__(self):
                 args = []
                 kwargs = self.args[0]
 
-        msg = str(self.msg)
+        msg = text_type(self.msg)
 
         try:
             return msg.format(*args, **kwargs)


Test patch: