Skip to content

Commit ae5a528

Browse files
Enables logging and verbosity control using KERAS_REMOTE_LOG_LEVEL env var (#87)
* enables absl logger * Adds env var * rich formatting * address reviews * Update README
1 parent e6ab84b commit ae5a528

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ train("gs://my-bucket/arrayrecords/")
331331
| `KERAS_REMOTE_ZONE` | No | `us-central1-a` | Default compute zone |
332332
| `KERAS_REMOTE_CLUSTER` | No | `keras-remote-cluster` | GKE cluster name |
333333
| `KERAS_REMOTE_GKE_NAMESPACE` | No | `default` | Kubernetes namespace |
334+
| `KERAS_REMOTE_LOG_LEVEL` | No | `INFO` | Log verbosity (`DEBUG`, `INFO`, `WARNING`, `ERROR`, `FATAL`) |
334335

335336
### Decorator Parameters
336337

@@ -450,11 +451,14 @@ gcloud builds list --project=$KERAS_REMOTE_PROJECT --limit=5
450451

451452
### Debug Logging
452453

453-
```python
454-
import logging
455-
logging.basicConfig(level=logging.INFO)
454+
Keras Remote uses `absl-py` for logging. You can control the log verbosity by setting the `KERAS_REMOTE_LOG_LEVEL` environment variable:
455+
456+
```bash
457+
export KERAS_REMOTE_LOG_LEVEL="DEBUG"
456458
```
457459

460+
Supported levels are `DEBUG`, `INFO`, `WARNING`, `ERROR`, and `FATAL`. The default is `INFO`.
461+
458462
### Verify Setup
459463

460464
```bash

keras_remote/__init__.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,46 @@
22

33
# Suppress noisy gRPC fork/logging messages before any gRPC imports
44
os.environ.setdefault("GRPC_VERBOSITY", "NONE")
5-
os.environ.setdefault("GLOG_minloglevel", "3")
65
os.environ.setdefault("GRPC_ENABLE_FORK_SUPPORT", "0")
76

7+
import logging as python_logging
8+
import os
9+
10+
from absl import logging
11+
from rich.console import Console
12+
from rich.logging import RichHandler
13+
814
from keras_remote.core.core import run as run
915
from keras_remote.data import Data as Data
16+
17+
logging.use_absl_handler()
18+
19+
# Use rich to format the absl logs, making them slightly dimmed and links clickable
20+
console = Console(stderr=True)
21+
rich_handler = RichHandler(
22+
console=console,
23+
show_time=False,
24+
show_path=False,
25+
show_level=False,
26+
markup=True,
27+
)
28+
rich_handler.setFormatter(python_logging.Formatter("[dim]%(message)s[/dim]"))
29+
30+
absl_logger = logging.get_absl_logger()
31+
absl_logger.handlers = [rich_handler]
32+
absl_logger.propagate = False
33+
34+
# Default to INFO if the user is running a script outside of absl.app.run()
35+
# This ensures that operations like container building and job status are visible.
36+
log_level = os.environ.get("KERAS_REMOTE_LOG_LEVEL", "INFO").upper()
37+
38+
if log_level == "DEBUG":
39+
logging.set_verbosity(logging.DEBUG)
40+
elif log_level == "INFO":
41+
logging.set_verbosity(logging.INFO)
42+
elif log_level == "WARNING":
43+
logging.set_verbosity(logging.WARNING)
44+
elif log_level == "ERROR":
45+
logging.set_verbosity(logging.ERROR)
46+
elif log_level == "FATAL":
47+
logging.set_verbosity(logging.FATAL)

0 commit comments

Comments
 (0)