Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
FastAPI-CRUD
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
arjun.b
FastAPI-CRUD
Commits
af5af7eb
Commit
af5af7eb
authored
Feb 06, 2023
by
arjun.b
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logging file added
parent
9079c3c0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
20 additions
and
54 deletions
+20
-54
app.py
+4
-1
scripts/constants/db_connect.py
+3
-1
scripts/logging/logger_conf.yml
+0
-14
scripts/logging/logging.py
+8
-34
scripts/services/main.py
+5
-4
No files found.
app.py
View file @
af5af7eb
...
@@ -3,4 +3,7 @@ import uvicorn
...
@@ -3,4 +3,7 @@ import uvicorn
from
scripts.config.application_config
import
port_no
from
scripts.config.application_config
import
port_no
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
uvicorn
.
run
(
"scripts.services.main:app"
,
port
=
int
(
port_no
),
reload
=
True
)
try
:
uvicorn
.
run
(
"scripts.services.main:app"
,
port
=
int
(
port_no
),
reload
=
True
)
except
Exception
as
e
:
print
(
str
(
e
))
scripts/constants/db_connect.py
View file @
af5af7eb
from
pymongo
import
MongoClient
import
logging
from
pymongo
import
MongoClient
from
scripts.config.application_config
import
mongo_uri
from
scripts.config.application_config
import
mongo_uri
from
scripts.utils.mongo_utils
import
create_collection
from
scripts.utils.mongo_utils
import
create_collection
...
@@ -15,6 +16,7 @@ def db_connect():
...
@@ -15,6 +16,7 @@ def db_connect():
print
(
"mongo connect successfully"
)
print
(
"mongo connect successfully"
)
return
database
return
database
except
Exception
as
e
:
except
Exception
as
e
:
logging
.
error
(
"database connection error"
)
print
(
str
(
e
))
print
(
str
(
e
))
...
...
scripts/logging/logger_conf.yml
deleted
100644 → 0
View file @
9079c3c0
logger
:
name
:
fastapi
level
:
DEBUG
handlers
:
-
type
:
RotatingFileHandler
max_bytes
:
100000000
back_up_count
:
5
-
type
:
SocketHandler
host
:
localhost
port
:
8000
-
type
:
StreamHandler
name
:
fastapi crud
scripts/logging/logging.py
View file @
af5af7eb
import
logging
import
logging
import
os
import
os
# from logging import StreamHandler
from
logging.handlers
import
RotatingFileHandler
from
logging.handlers
import
RotatingFileHandler
,
SocketHandler
import
yaml
from
scripts.config.application_config
import
file_path
from
scripts.config.application_config
import
file_path
# this method is to read the configuration from backup.conf
def
read_configuration
(
file_name
):
"""
:param file_name:
:return: all the configuration constants
"""
with
open
(
file_name
,
'r'
)
as
stream
:
try
:
return
yaml
.
safe_load
(
stream
)
except
Exception
as
e
:
print
(
f
"Failed to load Configuration. Error: {e}"
)
config
=
read_configuration
(
"scripts/logging/logger_conf.yml"
)
logging_config
=
config
[
"logger"
]
def
get_logger
():
def
get_logger
():
"""
"""
Creates a rotating log
Creates a rotating log
"""
"""
__logger__
=
logging
.
getLogger
(
''
)
__logger__
=
logging
.
getLogger
(
''
)
__logger__
.
setLevel
(
logging_config
[
"level"
]
.
upper
()
)
__logger__
.
setLevel
(
"ERROR"
)
log_formatter
=
'
%(asctime)
s -
%(levelname)-6
s - [
%(threadName)5
s:
%(funcName)5
s():
%(lineno)
s] -
%(message)
s'
log_formatter
=
'
%(asctime)
s -
%(levelname)-6
s - [
%(threadName)5
s:
%(funcName)5
s():
%(lineno)
s] -
%(message)
s'
time_format
=
"
%
Y-
%
m-
%
d
%
H:
%
M:
%
S"
time_format
=
"
%
Y-
%
m-
%
d
%
H:
%
M:
%
S"
formatter
=
logging
.
Formatter
(
log_formatter
,
time_format
)
formatter
=
logging
.
Formatter
(
log_formatter
,
time_format
)
log_file
=
os
.
path
.
join
(
f
"{file_path}ERROR.log"
)
temp_handler
=
RotatingFileHandler
(
log_file
,
maxBytes
=
100000000
,
backupCount
=
5
)
temp_handler
.
setFormatter
(
formatter
)
for
each_handler
in
logging_config
[
"handlers"
]:
__logger__
.
addHandler
(
temp_handler
)
if
each_handler
[
"type"
]
in
[
"RotatingFileHandler"
]:
if
not
os
.
path
.
exists
(
file_path
):
os
.
makedirs
(
file_path
)
log_file
=
os
.
path
.
join
(
f
"{file_path}{logging_config['name']}.log"
)
temp_handler
=
RotatingFileHandler
(
log_file
,
maxBytes
=
each_handler
[
"max_bytes"
],
backupCount
=
each_handler
[
"back_up_count"
])
temp_handler
.
setFormatter
(
formatter
)
__logger__
.
addHandler
(
temp_handler
)
return
__logger__
return
__logger__
...
...
scripts/services/main.py
View file @
af5af7eb
...
@@ -6,6 +6,7 @@ from scripts.constants.end_points import EndPoints
...
@@ -6,6 +6,7 @@ from scripts.constants.end_points import EndPoints
from
scripts.core.handlers.fastapi_handler
import
fastAPIHandler
from
scripts.core.handlers.fastapi_handler
import
fastAPIHandler
from
scripts.database.model
import
employee_insert
,
employee_update
from
scripts.database.model
import
employee_insert
,
employee_update
import
pandas
as
pd
import
pandas
as
pd
from
scripts.logging.logging
import
logger
app
=
FastAPI
()
app
=
FastAPI
()
connect
(
db
=
"employees"
,
host
=
"localhost"
,
port
=
int
(
port_connect
))
connect
(
db
=
"employees"
,
host
=
"localhost"
,
port
=
int
(
port_connect
))
...
@@ -24,7 +25,7 @@ async def upload_file(file: UploadFile):
...
@@ -24,7 +25,7 @@ async def upload_file(file: UploadFile):
fastAPIHandler
.
file_upload
(
file_content
)
fastAPIHandler
.
file_upload
(
file_content
)
return
{
"data"
:
"CSV data uploaded successfully"
}
return
{
"data"
:
"CSV data uploaded successfully"
}
except
Exception
as
e
:
except
Exception
as
e
:
logg
ing
.
error
(
f
'upload file failed:{e}'
)
logg
er
.
error
(
f
'upload file failed:{e}'
)
print
(
e
)
print
(
e
)
...
@@ -35,7 +36,7 @@ def send_data(emp: employee_insert):
...
@@ -35,7 +36,7 @@ def send_data(emp: employee_insert):
fastAPIHandler
.
insert_data
(
emp
)
fastAPIHandler
.
insert_data
(
emp
)
return
{
"message"
:
"new data has been inserted"
}
return
{
"message"
:
"new data has been inserted"
}
except
Exception
as
e
:
except
Exception
as
e
:
logg
ing
.
error
(
f
'inserting a document failed {e}'
)
logg
er
.
error
(
f
'inserting a document failed {e}'
)
# update data
# update data
...
@@ -45,7 +46,7 @@ def update_data(doc_id: int, details: employee_update):
...
@@ -45,7 +46,7 @@ def update_data(doc_id: int, details: employee_update):
fastAPIHandler
.
update_data
(
doc_id
,
details
)
fastAPIHandler
.
update_data
(
doc_id
,
details
)
return
{
"data"
:
"data updated"
}
return
{
"data"
:
"data updated"
}
except
Exception
as
e
:
except
Exception
as
e
:
logg
ing
.
error
(
f
'updating the data failed {e}'
)
logg
er
.
error
(
f
'updating the data failed {e}'
)
# delete data
# delete data
...
@@ -55,4 +56,4 @@ def delete_data(doc_id: int):
...
@@ -55,4 +56,4 @@ def delete_data(doc_id: int):
fastAPIHandler
.
delete_data
(
doc_id
)
fastAPIHandler
.
delete_data
(
doc_id
)
return
{
"data"
:
"data deleted"
}
return
{
"data"
:
"data deleted"
}
except
Exception
as
e
:
except
Exception
as
e
:
logg
ing
.
error
(
f
'deleting a document failed {e}'
)
logg
er
.
error
(
f
'deleting a document failed {e}'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment