logging
Methods for setting up logging for tools.¶
Progress Logging Examples¶
Frequently input data (SAM/BAM/CRAM/VCF) are iterated in genomic coordinate order. Logging
progress is useful to not only log how many inputs have been consumed, but also their genomic
coordinate. ProgressLogger() can log progress every
fixed number of records. Logging can be written to logging.Logger as well as custom print
method.
>>> from fgpyo.util.logging import ProgressLogger
>>> logged_lines = []
>>> progress = ProgressLogger(
... printer=lambda s: logged_lines.append(s),
... verb="recorded",
... noun="items",
... unit=2
... )
>>> progress.record(reference_name="chr1", position=1) # does not log
False
>>> progress.record(reference_name="chr1", position=2) # logs
True
>>> progress.record(reference_name="chr1", position=3) # does not log
False
>>> progress.log_last() # will log the last recorded item, if not previously logged
True
>>> logged_lines # show the lines logged
['recorded 2 items: chr1:2', 'recorded 3 items: chr1:3']
Classes¶
ProgressLogger ¶
Bases: AbstractContextManager
A little class to track progress.
This will output a log message every unit number times recorded.
Attributes:
| Name | Type | Description |
|---|---|---|
printer |
Callable[[str], Any]
|
either a Logger (in which case progress will be printed at Info) or a lambda that consumes a single string |
noun |
str
|
the noun to use in the log message |
verb |
str
|
the verb to use in the log message |
unit |
int
|
the number of items for every log message |
count |
int
|
the total count of items recorded |
Source code in fgpyo/util/logging.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
Functions¶
log_last ¶
Force logging the last record, for example when progress has completed.
Source code in fgpyo/util/logging.py
record ¶
Record an item at a given genomic coordinate. Args: reference_name: the reference name of the item position: the 1-based start position of the item Returns: true if a message was logged, false otherwise
Source code in fgpyo/util/logging.py
record_alignment ¶
Correctly record pysam.AlignedSegments (zero-based coordinates).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rec
|
AlignedSegment
|
pysam.AlignedSegment object |
required |
Returns:
| Type | Description |
|---|---|
bool
|
true if a message was logged, false otherwise |
Source code in fgpyo/util/logging.py
record_alignments ¶
Correctly record multiple pysam.AlignedSegments (zero-based coordinates).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recs
|
Iterable[AlignedSegment]
|
pysam.AlignedSegment objects |
required |
Returns:
| Type | Description |
|---|---|
bool
|
true if a message was logged, false otherwise |
Source code in fgpyo/util/logging.py
Functions¶
setup_logging ¶
Globally configure logging for all modules
Configures logging to run at a specific level and output messages to stderr with useful information preceding the actual log message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
the default level for the logger |
'INFO'
|
name
|
str
|
the name of the logger |
'fgpyo'
|