Skip to content

Commit 69acae0

Browse files
authored
Merge pull request #285 from nsidc/issue-272
Issue-272: Upgrade log file management
2 parents eb8275d + a8e9f69 commit 69acae0

File tree

17 files changed

+116
-19
lines changed

17 files changed

+116
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## UNRELEASED
22

33
* Issue-255: Remove CSV reader capability (generic & SNOWEX-specific readers)
4-
* Fix configuration issues with some integration tests
4+
* Issue-282: Fix configuration issues with some integration tests
5+
* Issue-272: Add a user-provided logfile directory location. Add the configuration
6+
file's basename and the metgenc start datetime to the logfile's name.
57

68
## v1.11.0 (2025-09-02)
79

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,13 @@ Example running **init**
326326

327327
$ metgenc init -c ./init/<name of config file to create or modify>.ini
328328

329-
##### INI RULES:
329+
##### INI RULES:
330330
* The .ini file's `checksum_type = SHA256` should never be edited
331331
* The `kinesis_stream_name` and `staging_bucket_name` should never be edited
332332
* `auth_id` and `version` must accurately reflect the collection's authID and versionID
333+
* `log_dir` specifies the directory where log files will be written. Log files are named `metgenc-{config-name}-{timestamp}.log` where config-name is the base name of the .ini file and timestamp is in YYYYMMDD-HHMM format. The log directory must exist and be writable. If not specified, defaults to `/share/logs/metgenc`
333334
* provider is a free text attribute where, for now, the version of metgenc being run should be documented
334-
* running `metgenc --version` will return the current version
335+
* running `metgenc --version` will return the current version
335336

336337
#### Required and Optional Configuration Elements
337338
Some attribute values may be read from the .ini file if the values
@@ -381,7 +382,7 @@ Note column:
381382
with multiple associated browse files work fine with MetGenC! The default is `_brws`, change it to reflect
382383
the browse file names of the data delivered. This element is prompted for when running `metgenc init`.
383384
3. The file name pattern to be used for multi-file granules to define a file name pattern to appropriately
384-
group files together as a granule using the elements common amongst their names.
385+
group files together as a granule using the elements common amongst their names.
385386
- This must result in a globally unique: product/name (in CNM), and Identifier (as the IdentifierType: ProducerGranuleId in UMM-G)
386387
generated for each granule. This init element value must be added manually as it's **not** included in the `metgenc init` prompts.
387388
5. The file name pattern identifying a single file for metgenc to reference as the primary
@@ -517,7 +518,7 @@ When a granule has an associated `.spatial` file containing geodetic point data
517518
| Spatial | spatial_polygon_cartesian_tolerance | float | 0.0001 | Minimum distance between polygon points in degrees (0.00001-0.01) |
518519

519520
##### Example Spatial Polygon Generation Configuration
520-
Example showing content added to an .ini file, having edited the CMR default vertex tolerance
521+
Example showing content added to an .ini file, having edited the CMR default vertex tolerance
521522
(distance between two vertices) to decrease the precision of the GPoly coordinate pairs listed
522523
in the UMMG json files MetGenC generates:
523524
```ini
@@ -590,6 +591,7 @@ Using configuration:
590591
+ write_cnm_file: True
591592
+ overwrite_ummg: True
592593
+ checksum_type: SHA256
594+
+ log_dir: /share/logs/metgenc
593595
+ number: 1000000
594596
+ dry_run: False
595597
+ premet_dir: None

fixtures/test.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ overwrite_ummg = True
2626

2727
[Settings]
2828
checksum_type = SHA256
29+
log_dir = /tmp

src/nsidc/metgen/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def info(config_filename):
5656
configuration = config.configuration(
5757
config.config_parser_factory(config_filename), {}
5858
)
59-
metgen.init_logging()
59+
metgen.init_logging(configuration)
6060
configuration.show()
6161

6262

@@ -82,7 +82,7 @@ def validate(config_filename, content_type):
8282
configuration = config.configuration(
8383
config.config_parser_factory(config_filename), {}
8484
)
85-
metgen.init_logging()
85+
metgen.init_logging(configuration)
8686
metgen.validate(configuration, content_type)
8787

8888

@@ -146,7 +146,7 @@ def process(config_filename, dry_run, env, number, write_cnm, overwrite):
146146
configuration = config.configuration(
147147
config.config_parser_factory(config_filename), overrides, env
148148
)
149-
metgen.init_logging()
149+
metgen.init_logging(configuration)
150150
configuration.show()
151151
config.validate(configuration)
152152
config.validate_spatial_source(configuration)

src/nsidc/metgen/config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Config:
5050
spatial_polygon_target_coverage: Optional[float] = None
5151
spatial_polygon_max_vertices: Optional[int] = None
5252
spatial_polygon_cartesian_tolerance: Optional[float] = None
53+
log_dir: Optional[str] = None
54+
name: Optional[str] = None
5355

5456
def show(self):
5557
# TODO: add section headings in the right spot
@@ -87,6 +89,10 @@ def config_parser_factory(configuration_file):
8789
# If the config parser gets no value (empty string), interpret it as False
8890
cfg_parser.BOOLEAN_STATES |= [("", False)]
8991
cfg_parser.read(configuration_file)
92+
93+
# Store the config file basename for use in logging
94+
cfg_parser._config_name = os.path.splitext(os.path.basename(configuration_file))[0]
95+
9096
return cfg_parser
9197

9298

@@ -148,6 +154,7 @@ def configuration(
148154
"spatial_polygon_target_coverage": constants.DEFAULT_SPATIAL_POLYGON_TARGET_COVERAGE,
149155
"spatial_polygon_max_vertices": constants.DEFAULT_SPATIAL_POLYGON_MAX_VERTICES,
150156
"spatial_polygon_cartesian_tolerance": constants.DEFAULT_SPATIAL_POLYGON_CARTESIAN_TOLERANCE,
157+
"log_dir": constants.DEFAULT_LOG_DIR,
151158
}
152159
try:
153160
return Config(
@@ -318,6 +325,15 @@ def configuration(
318325
config_parser,
319326
overrides,
320327
),
328+
_get_configuration_value(
329+
environment,
330+
"Settings",
331+
"log_dir",
332+
str,
333+
config_parser,
334+
overrides,
335+
),
336+
getattr(config_parser, "_config_name", "metgenc"),
321337
)
322338
except Exception as e:
323339
raise Exception("Unable to read the configuration file", e)
@@ -390,6 +406,13 @@ def validate(configuration):
390406
else True,
391407
"The spatial polygon cartesian tolerance must be between 0.00001 and 0.01 degrees.",
392408
],
409+
[
410+
"log_dir",
411+
lambda log_dir: os.path.exists(log_dir) and os.access(log_dir, os.W_OK)
412+
if log_dir
413+
else True,
414+
"The log directory does not exist or is not writable.",
415+
],
393416
]
394417
errors = [
395418
msg for name, fn, msg in validations if not fn(getattr(configuration, name))

src/nsidc/metgen/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
DEFAULT_BROWSE_REGEX = "_brws"
1616
DEFAULT_COLLECTION_GEOMETRY_OVERRIDE = False
1717
DEFAULT_COLLECTION_TEMPORAL_OVERRIDE = False
18+
DEFAULT_LOG_DIR = "/share/logs/metgenc"
1819

1920
# Spatial polygon defaults
2021
DEFAULT_SPATIAL_POLYGON_ENABLED = True

src/nsidc/metgen/metgen.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# -------------------------------------------------------------------
5353

5454

55-
def init_logging():
55+
def init_logging(configuration=None):
5656
"""
5757
Initialize the logger for metgenc.
5858
"""
@@ -64,7 +64,21 @@ def init_logging():
6464
console_handler.setFormatter(logging.Formatter(CONSOLE_FORMAT))
6565
logger.addHandler(console_handler)
6666

67-
logfile_handler = logging.FileHandler(constants.ROOT_LOGGER + ".log", "a")
67+
# Generate log filename
68+
log_dir = constants.DEFAULT_LOG_DIR
69+
if configuration and configuration.log_dir:
70+
log_dir = configuration.log_dir
71+
72+
# Generate filename: metgenc-{name}-{datetime}.log
73+
config_basename = "metgenc"
74+
if configuration and configuration.name:
75+
config_basename = configuration.name
76+
77+
timestamp = dt.datetime.now().strftime("%Y%m%d-%H%M")
78+
log_filename = f"metgenc-{config_basename}-{timestamp}.log"
79+
log_path = os.path.join(log_dir, log_filename)
80+
81+
logfile_handler = logging.FileHandler(log_path, "a")
6882
logfile_handler.setLevel(logging.DEBUG)
6983
logfile_handler.setFormatter(logging.Formatter(LOGFILE_FORMAT))
7084
logger.addHandler(logfile_handler)
@@ -225,6 +239,11 @@ def init_config(configuration_file):
225239
"checksum_type",
226240
Prompt.ask("Checksum type", default=constants.DEFAULT_CHECKSUM_TYPE),
227241
)
242+
cfg_parser.set(
243+
constants.SETTINGS_SECTION_NAME,
244+
"log_dir",
245+
Prompt.ask("Log directory", default=constants.DEFAULT_LOG_DIR),
246+
)
228247

229248
print()
230249
print(f"Saving new configuration: {configuration_file}")

tests/integration/configs/IPFLT1B_DUCk.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ overwrite_ummg = True
2222

2323
[Settings]
2424
checksum_type = SHA256
25+
log_dir = ./output/logs

tests/integration/configs/IRTIT3DUCk.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ overwrite_ummg = False
1919

2020
[Settings]
2121
checksum_type = SHA256
22+
log_dir = ./output/logs

tests/integration/configs/IRWIS2DUCk.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ overwrite_ummg = false
2121

2222
[Settings]
2323
checksum_type = output
24+
log_dir = ./output/logs

0 commit comments

Comments
 (0)