forked from shotgunsoftware/tk-framework-adobe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.py
More file actions
64 lines (49 loc) · 2.13 KB
/
Copy pathlog.py
File metadata and controls
64 lines (49 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright (c) 2019 Shotgun Software Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Shotgun Software Inc.
import os
import logging
class BootstrapLogHandler(logging.StreamHandler):
"""
Manually flushes emitted records for js to pickup.
"""
def emit(self, record):
"""
Forwards the record back to to js via the engine communicator.
:param record: The record to log.
"""
# can't use super here because in python 2.6, logging.StreamHandler is
# not a new style class.
logging.StreamHandler.emit(self, record)
# always flush to ensure its seen by the js process
self.flush()
def get_sgtk_logger(sgtk):
"""
Sets up a std log handler for toolkit
:param sgtk: An sgtk module reference.
:returns: A log handler.
"""
# add a custom handler to the root logger so that all toolkit log messages
# are forwarded back to python via the communicator
bootstrap_log_formatter = logging.Formatter("[%(levelname)s]: %(message)s")
bootstrap_log_handler = BootstrapLogHandler()
bootstrap_log_handler.setFormatter(bootstrap_log_formatter)
if sgtk.LogManager().global_debug:
bootstrap_log_handler.setLevel(logging.DEBUG)
else:
bootstrap_log_handler.setLevel(logging.INFO)
# now get a logger to use during bootstrap
sgtk.LogManager().initialize_custom_handler(bootstrap_log_handler)
# the log should go to a file named like the engine, which is to be started
# if the engine is unknown to the current environment, it will just log
# into the frameworks own log-file
log_name = os.getenv("SHOTGUN_ENGINE", "tk-framework-adobe")
# initializes the file where logging output will go
sgtk.LogManager().initialize_base_file_handler(log_name)
return bootstrap_log_handler