-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
169 lines (154 loc) · 5.4 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
import os
import logging
import logging.config
# Base configuration class
class Config:
SECRET_KEY = os.getenv('SECRET_KEY', 'your_secret_key')
SQLALCHEMY_TRACK_MODIFICATIONS = False
JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY', 'your_jwt_secret_key')
API_VERSION = os.getenv('API_VERSION', 'v1')
# Default log file path and creation
LOG_FILE_PATH = os.getenv('LOG_FILE_PATH', 'logs/')
if not os.path.exists(LOG_FILE_PATH):
os.makedirs(LOG_FILE_PATH)
# Common Logging configuration
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
},
},
'handlers': {
'general_file': {
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_FILE_PATH, 'general.log'),
'formatter': 'default',
},
'error_file': {
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_FILE_PATH, 'error.log'),
'formatter': 'default',
'level': 'ERROR',
},
'success_file': {
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_FILE_PATH, 'success.log'),
'formatter': 'default',
'level': 'INFO',
},
},
'loggers': {
'general': {
'handlers': ['general_file'],
'level': 'INFO',
'propagate': True,
},
'error': {
'handlers': ['error_file'],
'level': 'ERROR',
'propagate': True,
},
'success': {
'handlers': ['success_file'],
'level': 'INFO',
'propagate': True,
},
},
}
# Development Configuration
class DevelopmentConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv('DEV_DATABASE_URL', 'sqlite:///dev.db')
CORS_ORIGINS = ["http://localhost:3000", "http://localhost:5000", "http://127.0.0.1:3000", "http://127.0.0.1:5000"]
DEBUG = True
TESTING = True
# Override logging for development (stdout, more verbose)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
},
'general_file': {
'class': 'logging.FileHandler',
'filename': os.path.join(Config.LOG_FILE_PATH, 'dev_general.log'),
'formatter': 'default',
},
'error_file': {
'class': 'logging.FileHandler',
'filename': os.path.join(Config.LOG_FILE_PATH, 'dev_error.log'),
'formatter': 'default',
'level': 'ERROR',
},
},
'loggers': {
'general': {
'handlers': ['console', 'general_file'],
'level': 'DEBUG',
'propagate': True,
},
'error': {
'handlers': ['console', 'error_file'],
'level': 'ERROR',
'propagate': True,
},
},
}
# Production Configuration
class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', 'postgresql://root:Sales_Password1@localhost/sales_app_db')
CORS_ORIGINS = ["https://salesapp.impactlife.com.gh", "https://api.salesapp.impactlife.com.gh", "http://localhost:3000", "http://127.0.0.1:3000"]
DEBUG = False
TESTING = False
# Test Configuration (Same as Development but with a separate test database)
class TestConfig(DevelopmentConfig):
SQLALCHEMY_DATABASE_URI = os.getenv('TEST_DATABASE_URL', 'sqlite:///test.db')
TESTING = True
DEBUG = True
# You can disable or minimize logging during tests to focus on test outputs
LOGGING = {
'version': 1,
'disable_existing_loggers': True, # Disable logging for faster test runs
'formatters': {
'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
},
},
'loggers': {
'general': {
'handlers': ['console'],
'level': 'WARNING', # Log only warnings and errors during tests
'propagate': True,
},
'error': {
'handlers': ['console'],
'level': 'ERROR',
'propagate': True,
},
},
}
# Initialize logging configuration
def setup_logging():
env = os.getenv('FLASK_ENV', 'production') # Default to production
if env == 'development':
logging.config.dictConfig(DevelopmentConfig.LOGGING)
elif env == 'testing':
logging.config.dictConfig(TestConfig.LOGGING)
else:
logging.config.dictConfig(ProductionConfig.LOGGING)
# Ensure logging is set up at module load
logging.config.dictConfig(Config.LOGGING)