|
import logging |
|
|
|
import streamlit as st |
|
from streamlit import runtime |
|
from streamlit.runtime.scriptrunner import get_script_run_ctx |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import subprocess |
|
|
|
def get_remote_ip(): |
|
try: |
|
result = subprocess.run(['whoami'], capture_output=True, text=True) |
|
username = result.stdout.strip() |
|
return username |
|
except Exception as e: |
|
return str(e) |
|
|
|
|
|
st.write(get_remote_ip()) |
|
|
|
|
|
class ContextFilter(logging.Filter): |
|
def filter(self, record): |
|
record.user_ip = get_remote_ip() |
|
return super().filter(record) |
|
|
|
def init_logging(): |
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("foobar") |
|
if logger.handlers: |
|
return |
|
logger.propagate = False |
|
logger.setLevel(logging.INFO) |
|
|
|
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s [user_ip=%(user_ip)s] - %(message)s") |
|
handler = logging.StreamHandler() |
|
handler.setLevel(logging.INFO) |
|
handler.addFilter(ContextFilter()) |
|
handler.setFormatter(formatter) |
|
logger.addHandler(handler) |
|
|
|
def main(): |
|
logger.info("Inside main") |
|
st.title("Title") |
|
|
|
text = st.sidebar.text_input("Text:") |
|
logger.info(f"This is the text: {text}") |
|
|
|
if __name__ == "__main__": |
|
init_logging() |
|
|
|
logger = logging.getLogger("foobar") |
|
main() |