Skip to content

Commit 45f2fa2

Browse files
authored
Merge pull request #4 from cedadev/origin/ioana.circu-logging
ioana.circu - logging
2 parents db54d04 + 3c826d9 commit 45f2fa2

File tree

6 files changed

+229
-30
lines changed

6 files changed

+229
-30
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ settings.json
33
local/
44
localcache/
55
*pyc
6-
build/
6+
build/

dirconfig

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Directory containing new flights to be pushed to Elasticsearch
2-
/home/badc/software/datasets/flight-finder/add_records/
2+
/home/users/icircu/flight-pipeline/add/
33
# Directory for moving written flights - write DELETE to remove pushed flight records from the local system after pushing.
4-
/home/badc/software/datasets/flight-finder/stac-flightfinder-items/
4+
/home/users/icircu/test-flights/
5+
# Logging File
6+
/home/users/icircu/test-flights/logging

flight_update.py

+21-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import importlib
99

1010
import logging
11+
from flightpipe.logger import logger
1112

1213
import argparse
1314

@@ -18,7 +19,6 @@
1819

1920
settings_file = 'settings.json'
2021

21-
2222
def openConfig():
2323
"""
2424
Function to open configuration file and initialise paths to relevant directories.
@@ -31,14 +31,15 @@ def openConfig():
3131

3232
if VERB:
3333
print('> (1/6) Opening Config File')
34-
logging.info('> (1/6) Opening Config File')
34+
logger.info('> (1/6) Opening Config File')
3535

3636
f = open('dirconfig','r')
3737
content = f.readlines()
3838
f.close()
3939
try:
40-
return content[1].replace('\n',''), content[3].replace('\n',''), content[5].replace('\n','')
40+
return content[1].replace('\n',''), content[3].replace('\n','')
4141
except IndexError:
42+
logger.error('One or both paths missing from the dirconfig file')
4243
print('Error: One or both paths missing from the dirconfig file - please fill these in')
4344
return '',''
4445

@@ -73,7 +74,7 @@ def addFlights(rootdir, archive, repush=False):
7374
# ES client to determine array of ids
7475
if VERB:
7576
print('> (2/6) Setting up ES Flight Client')
76-
logging.info('> (2/6) Setting up ES Flight Client')
77+
logger.info('> (2/6) Setting up ES Flight Client')
7778
if repush:
7879
files_list = os.listdir(archive)
7980
fclient = ESFlightClient(archive, settings_file)
@@ -87,21 +88,21 @@ def addFlights(rootdir, archive, repush=False):
8788
# Push new flights to index
8889
if VERB:
8990
print('> (4/6) Identified {} flights'.format(len(checked_list)))
90-
logging.info('> (4/6) Identified {} flights'.format(len(checked_list)))
91+
logger.info('> (4/6) Identified {} flights'.format(len(checked_list)))
9192
if len(checked_list) > 0:
9293
fclient.push_flights(checked_list)
9394
if VERB:
9495
print('> (5/6) Pushed flights to ES Index')
95-
logging.info('> (5/6) Pushed flights to ES Index')
96+
logger.info('> (5/6) Pushed flights to ES Index')
9697
if not repush:
9798
moveOldFiles(rootdir, archive, checked_list)
9899
if VERB:
99100
print('> (6/6) Removed local files from push directory')
100-
logging.info('> (6/6) Removed local files from push directory')
101+
logger.info('> (6/6) Removed local files from push directory')
101102
else:
102103
if VERB:
103104
print('> Exiting flight pipeline')
104-
logging.info('> Exiting flight pipeline')
105+
logger.info('> Exiting flight pipeline')
105106

106107
# Move old records into an archive directory
107108

@@ -136,27 +137,19 @@ def main():
136137
REPUSH = False
137138

138139
if args.mode == 'add':
139-
root, archive, log_file = openConfig()
140-
141-
if log_file == '':
142-
print("Error: Please fill in the third directory in dirconfig file")
143-
144-
# Set up logging config
145-
logging.basicConfig(
146-
level=logging.DEBUG, # Capture all levels
147-
format='%(asctime)s - %(levelname)s - %(message)s', # timestamp, level, message
148-
handlers=[
149-
logging.FileHandler(log_file), # Write output to file
150-
logging.StreamHandler() # If logging to console
151-
]
152-
)
140+
logger.debug("Mode set to add")
141+
root, archive = openConfig()
142+
143+
logger.debug("Root directory set to %s", root)
144+
logger.debug("Archive set to %s", archive)
145+
153146
if archive == '':
154147
print('Error: Please fill in second directory in dirconfig file')
155-
logging.error("Error: Second directory in dirconfig file missing")
148+
logger.error("Second directory in dirconfig file missing")
156149
sys.exit()
157150
elif root == '':
158151
print('Error: Please fill in first directory in dirconfig file')
159-
logging.error("Error: First directory in dirconfig file missing")
152+
logger.error("First directory in dirconfig file missing")
160153
sys.exit()
161154
else:
162155
addFlights(root, archive, repush=REPUSH)
@@ -171,14 +164,18 @@ def main():
171164
"""
172165

173166
elif args.mode == 'update':
167+
logger.debug("Mode set to update")
174168
updateFlights(args.update)
175169

176170
elif args.mode == 'add_moles':
171+
logger.debug("Mode set to add moles")
177172
updateFlights('moles')
178173

179174
elif args.mode == 'reindex':
175+
logger.debug("Mode set to reindex")
180176
reindex(args.new_index)
181177
else:
178+
logger.error("Mode unrecognised - ", args.mode)
182179
print('Error: Mode unrecognised - ', args.mode)
183180
sys.exit()
184181

flightpipe/flight_client.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
from elasticsearch import Elasticsearch
1212
from elasticsearch.helpers import bulk
1313

14-
from ceda_elastic_py import SimpleClient, gen_id
14+
from flightpipe.logger import logger
15+
16+
from flightpipe.simple_client import SimpleClient, gen_id
17+
from flightpipe.logger import setup_logging
1518

1619
from datetime import datetime
1720

@@ -22,6 +25,7 @@
2225
urllib3.disable_warnings()
2326

2427
def resolve_link(path, ):
28+
logger.debug("Debug: Resolving link for path %s", path)
2529
mpath = str(path)
2630

2731
uuid = None
@@ -32,26 +36,31 @@ def resolve_link(path, ):
3236
r = json.loads(resp)
3337
if r['results']:
3438
uuid = r['results'][0]['uuid']
39+
logger.debug("Debug: Reslolving link, found uuid %s", str(uuid))
3540
except:
3641
print(f'Unsuccessful link retrieval for {path} - proceeding without')
3742
path = '/'.join(path.split('/')[:-1])
3843

3944
if not uuid:
45+
logger.error("Error: Recursive path search failed for %s", mpath)
4046
print(f'Recursive path search failed for: {mpath}')
4147

4248
return uuid
4349

4450
class ESFlightClient(SimpleClient):
4551
"""
4652
Connects to an elasticsearch instance and exports the
47-
documents to elasticsearch."""
53+
documents to elasticsearch.
54+
"""
4855

4956
def __init__(self, rootdir, es_config='settings.json'):
5057
self.rootdir = rootdir
58+
logger.info("Info: Initialising ES Flight Client")
5159

52-
super().__init__("stac-flightfinder-items", es_config=es_config)
60+
super().__init__("stac-flightfinder-items-test", es_config=es_config)
5361

5462
with open('stac_template.json') as f:
63+
logger.info("Info: Reading stac templace JSON file")
5564
self.required_keys = json.load(f).keys()
5665

5766
def push_flights(self, file_list):
@@ -63,10 +72,13 @@ def push_flights(self, file_list):
6372
elif isinstance(file_list[0], dict):
6473
flight_list = file_list
6574
else:
75+
logger.error("Error: Flight file not found %s", str(file_list[0]))
6676
raise FileNotFoundError(file_list[0])
77+
logger.info("Info: Flights to be pushed %s", str(flight_list))
6778
self.push_records(flight_list)
6879

6980
def preprocess_records(self, file_list):
81+
logger.debug("Debug: Processing following records - %s", file_list)
7082

7183
def set_defaults(refs):
7284
collection = refs['collection']
@@ -116,6 +128,7 @@ def set_defaults(refs):
116128
if rq not in source:
117129
missing.append(rq)
118130
if len(missing) > 0:
131+
logger.error("Error: File is missing entries - %s", str(missing))
119132
raise TypeError(f"File {file} is missing entries:{missing}")
120133

121134
source['last_update'] = datetime.strftime(datetime.now(),'%Y-%m-%d %H:%M:%S')
@@ -126,6 +139,7 @@ def set_defaults(refs):
126139
return records
127140

128141
def obtain_field(self, id, fieldnames):
142+
logger.info("Info: Performing query to obtain the following fields: %s", str(fieldnames))
129143
search = {
130144
"_source": fieldnames,
131145
"query": {
@@ -140,11 +154,14 @@ def obtain_field(self, id, fieldnames):
140154
body=search)
141155

142156
try:
157+
logger.info("Info: Found following fields: %s", str(resp['hits']['hits'][0]))
143158
return resp['hits']['hits'][0]
144159
except IndexError: # No entry found
160+
logger.error("Error: No entry found.")
145161
return None
146162

147163
def add_field(self, id, data, fieldname):
164+
logger.debug("Debug: Update mapping for id - %s", str(id))
148165
# Update mapping
149166
self.es.update(index=self.index, doc_type='_doc', id=id, body={'doc':{fieldname:data}})
150167

@@ -227,6 +244,7 @@ def check_ptcode(self, ptcode):
227244
return 100
228245

229246
def reindex(self, new_index):
247+
logger.debug("Debug: Reindex for source %s and destination %s", self.index, new_index)
230248

231249
self.es.reindex({
232250
"source":{

flightpipe/logger.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import logging
2+
3+
def setup_logging(enable_logging=True, console_logging=True):
4+
"""
5+
Sets up logging configuration. If `enable_logging` is False, no logging will occur.
6+
7+
:param enable_logging: Flag to enable/disable logging.
8+
"""
9+
file = "dirconfig"
10+
11+
with open(file) as f: # 'r' is default if not specified.
12+
content = [r.strip() for r in f.readlines()] # Removes the '\n' from all lines
13+
14+
log_file = content[5].replace('\n','')
15+
16+
if log_file == '':
17+
print("Error: Please fill in the third directory in dirconfig file")
18+
19+
handlers = [
20+
logging.FileHandler(log_file), # Write output to file
21+
]
22+
23+
if console_logging:
24+
handlers.append(logging.StreamHandler()) # Logs to the console if enabled
25+
26+
27+
if enable_logging:
28+
logging.basicConfig(
29+
level=logging.DEBUG, # Capture all levels
30+
format='%(asctime)s - %(levelname)s - %(message)s',
31+
handlers=handlers
32+
)
33+
else:
34+
# Disable logging by setting a null handler
35+
logging.basicConfig(level=logging.CRITICAL)
36+
#NOTSET for no alerts at all
37+
38+
39+
enable_logging = True
40+
41+
# Set up logging with a flag (True to enable logging, False to disable logging)
42+
setup_logging(enable_logging) # Change to False to disable logging
43+
44+
logger = logging.getLogger(__name__)
45+
46+
47+
48+
__author__ = "Ioana Circu"
49+
__contact__ = "[email protected]"
50+
__copyright__ = "Copyright 2025 United Kingdom Research and Innovation"

0 commit comments

Comments
 (0)