Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
IDATx2001-2020
Contacts
Commits
b62bccdb
Commit
b62bccdb
authored
Mar 31, 2020
by
Arne Styve
Browse files
Added support for logging to a file (from the Logger)
parent
8d484456
Changes
4
Hide whitespace changes
Inline
Side-by-side
Contacts.iml
View file @
b62bccdb
...
...
@@ -5,12 +5,24 @@
<map
/>
</option>
</component>
<component
name=
"FacetManager"
>
<facet
type=
"jpa"
name=
"JPA"
>
<configuration>
<setting
name=
"validation-enabled"
value=
"true"
/>
<datasource-mapping>
<factory-entry
name=
"contacts-pu"
/>
</datasource-mapping>
<naming-strategy-map
/>
<deploymentDescriptor
name=
"persistence.xml"
url=
"file://$MODULE_DIR$/src/META-INF/persistence.xml"
/>
</configuration>
</facet>
</component>
<component
name=
"NewModuleRootManager"
inherit-compiler-output=
"true"
>
<exclude-output
/>
<content
url=
"file://$MODULE_DIR$"
>
<sourceFolder
url=
"file://$MODULE_DIR$/src"
isTestSource=
"false"
/>
</content>
<orderEntry
type=
"
jdk"
jdkName=
"1.8.0_201"
jdkType=
"JavaSDK
"
/>
<orderEntry
type=
"
inheritedJdk
"
/>
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
<orderEntry
type=
"library"
name=
"libs"
level=
"project"
/>
</component>
...
...
src/META-INF/persistence.xml
View file @
b62bccdb
...
...
@@ -17,7 +17,7 @@
<!-- Alternatives: create-tables, drop-and-create-tables-->
<property
name=
"eclipselink.ddl-generation"
value=
"create-tables"
/>
<!-- Alternatives: FINE (logs all SQL), ALL, CONFIG, INFO, WARNING..., OFF -->
<property
name=
"eclipselink.logging.level"
value=
"
FINE
"
/>
<property
name=
"eclipselink.logging.level"
value=
"
OFF
"
/>
<!--
The Database can be pre-filled with entries during startup. This would be very useful during testing
...
...
src/no/ntnu/idata2001/contacts/model/AddressBookDBHandler.java
View file @
b62bccdb
...
...
@@ -3,6 +3,8 @@ package no.ntnu.idata2001.contacts.model;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
...
...
@@ -20,12 +22,12 @@ import javax.persistence.Query;
* communication with the underlying RDBMS. Creating an EntityManagerFactory instance is costly:</p>
*
* <p>(<a href="https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html_single/#d0e980">)
*
"An entity manager factory is typically create at application initialization time and closed at
*
* application end. It's creation is an expensive process."</a></p>
* "An entity manager factory is typically create at application initialization time and closed at
* * application end. It's creation is an expensive process."</a></p>
* <p>
*
Hence the EMF is implemented as a field in this class.
* Hence the EMF is implemented as a field in this class.
* </p>
*
<p><b>NOTE:</b> While the EMF is <b>thread safe</b>, the EM is <b>not thread safe</b>.</p>
* <p><b>NOTE:</b> While the EMF is <b>thread safe</b>, the EM is <b>not thread safe</b>.</p>
*/
public
class
AddressBookDBHandler
implements
AddressBook
{
...
...
@@ -36,6 +38,9 @@ public class AddressBookDBHandler implements AddressBook {
// communicate with the database, and should hence not be serialized.
private
final
transient
EntityManagerFactory
efact
;
// Create a logger to be used within this class
private
final
transient
Logger
logger
=
Logger
.
getLogger
(
this
.
getClass
().
getName
());
/**
* Creates an instance of the AddressBookDBHandler.
*/
...
...
@@ -50,26 +55,55 @@ public class AddressBookDBHandler implements AddressBook {
eman
.
persist
(
contact
);
eman
.
getTransaction
().
commit
();
eman
.
close
();
this
.
logger
.
log
(
Level
.
INFO
,
()
->
"A contact was added to the DB. Name = "
+
contact
.
getName
()
+
" Phone = "
+
contact
.
getPhone
());
}
@Override
public
void
removeContact
(
String
phoneNumber
)
{
//TODO: To be implemented later...
EntityManager
eman
=
this
.
efact
.
createEntityManager
();
eman
.
getTransaction
().
begin
();
String
sql
=
"DELETE FROM ContactDetails c WHERE c.phone LIKE :contactPhone"
;
Query
query
=
eman
.
createQuery
(
sql
).
setParameter
(
"contactPhone"
,
phoneNumber
);
int
n
=
query
.
executeUpdate
();
eman
.
getTransaction
().
commit
();
// A quick note on this way of logging a message where we need to use
// string concatenation: String concatenation is "expensive". Hence we should
// avoid having to build the string if it is not going to be used, i.e. if the
// logging level is set to lower than INFO, this log-message will never be logged
// even though the string concatenation always will been performed.
// One solution, is to use lambda like shown below. This way we ensure that the lambda
// expression will not be executed unless the logging level is set to INFO or higher.
// Another alternative is to use a formatter.
this
.
logger
.
log
(
Level
.
INFO
,
()
->
"Removed contact with phone = "
+
phoneNumber
+
". EM returned "
+
n
+
" object deleted."
);
eman
.
close
();
}
@Override
public
Collection
<
ContactDetails
>
getAllContacts
()
{
EntityManager
eman
=
this
.
efact
.
createEntityManager
();
List
<
ContactDetails
>
contactsList
=
null
;
String
sql
=
"SELECT c FROM ContactDetails c"
;
Query
query
=
eman
.
createQuery
(
sql
);
contactsList
=
query
.
getResultList
();
List
<
ContactDetails
>
contactsList
=
query
.
getResultList
();
eman
.
close
();
this
.
logger
.
log
(
Level
.
INFO
,
()
->
"Read all contacts from the DB, a total of "
+
contactsList
.
size
());
return
contactsList
;
}
...
...
src/no/ntnu/idata2001/contacts/views/ContactsApp.java
View file @
b62bccdb
package
no.ntnu.idata2001.contacts.views
;
import
java.io.IOException
;
import
java.util.logging.FileHandler
;
import
java.util.logging.Handler
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javafx.application.Application
;
import
javafx.collections.FXCollections
;
import
javafx.collections.ObservableList
;
...
...
@@ -50,6 +55,15 @@ public class ContactsApp extends Application {
* @param args command line arguments provided during startup. Not used in this app.
*/
public
static
void
main
(
String
[]
args
)
{
// Setup logging to file, and the level to FINEST
try
{
Handler
fh
=
new
FileHandler
(
"./contacts.log"
);
Logger
.
getLogger
(
"no.ntnu.idata2001.contacts"
).
addHandler
(
fh
);
Logger
.
getLogger
(
"no.ntnu.idata2001.contacts"
).
setLevel
(
Level
.
FINEST
);
}
catch
(
IOException
e
)
{
Logger
.
getLogger
(
ContactsApp
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
"Could not create a log-file."
);
}
launch
(
args
);
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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